Have you ever wondered how an email message is programmatically sent out from an application? Here I will show three different ways that sent email can be coded in different software platforms.

The first programming snippet comes from a Windows Phone mobile app. It’s actually quite simple. This is for Windows Phone 8 and the C# Sharp coding. An email account needs to first be set up on the phone.

  1. view plainprint?
  2. // create email object.
  3. EmailComposeTask emailComposeTask = new EmailComposeTask();
  4. // add a subject.
  5. emailComposeTask.Subject = "test subject";
  6. // add a message.
  7. emailComposeTask.Body = "test message";
  8. // add a recipient.
  9. emailComposeTask.To = "[email protected]";
  10. // add a carbon copy recipient.
  11. emailComposeTask.Cc = "[email protected]";
  12. // add a blind carbon copy recipient.
  13. emailComposeTask.Bcc = "[email protected]";
  14. // bring up the email interface.
  15. emailComposeTask.Show();
Next, I will show how to transmit an email in PHP coding. This is taken from the email contact page on my website and it works very reliably. This does not require an email client to be set up.
  1. <?php
  2. session_start();
  3. // include this php file to process the ‘captcha’ code to verify the
  4. // sender is human, not a robot.
  5. include("simple-php-captcha.php");
  6. // initialize php error variables to check for valid fields of information.
  7. $flagvar = 0;
  8. $flagvar1 = 0;
  9. $flagvar2 = 0;
  10. $flagvar3 = 0;
  11. $flagvarx = 0;
  12. // assign the auto generated ‘captcha’ code to a
  13. // php session variable used to display it to the email sender.
  14. $_SESSION['captcha'] = captcha();
  15. // enter here after the user clicks the ‘Submit’ button
  16. // to send out the email.
  17. if ($_POST['submit_email']) {
  18. // check the sender’s email address to make sure it is valid and set error variables.
  19. if(strlen($_POST['EMAIL']) <= 0) {
  20. $flagvar1 = 1;
  21. $flagvar = 1;
  22. } else if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $_POST['EMAIL'])) {
  23. $flagvar1 = 1;
  24. $flagvar = 1;
  25. }
  26. // make sure there is something in the subject line and set error variables.
  27. if ( emptyempty($_POST['SUBJECT']) ) {
  28. $flagvar2 = 1;
  29. $flagvar = 1;
  30. }
  31. // make sure there is something in the message body and set error variables.
  32. if ( emptyempty($_POST['MESSAGEBOARD']) ) {
  33. $flagvar3 = 1;
  34. $flagvar = 1;
  35. }
  36. // ‘captcha’ codes match, then proceed.
  37. if ( $_REQUEST['skip_CaptchaCode'] === $_SESSION['firstcaptcha'] && $flagvar == 0 ) {
  38. // take out all html and php tags from the message body.
  39. $text = strip_tags($_POST['MESSAGEBOARD']);
  40. // concatenate two line feeds followed by the text “Email Sender”
  41. // followed by the sender’s email address.
  42. $text = $text."\n\nEmail Sender: ".$_POST['EMAIL'];
  43. // set the headers to nothing.
  44. $headers = "";
  45. // use the php mail function to send the email to my email address so
  46. // I can receive inquiries from prospective customers.
  47. mail('[email protected]', $_POST['SUBJECT'], $text, $headers);
  48. // display my web page to notify the sender that the submitted email has
  49. // been successfully sent to me.
  50. header("Location: http://www.analyzohiosoftware.com/email-verify.html");
  51. }
  52. // ‘captcha’ codes do not match, so set an error flag.
  53. if ( $_REQUEST['skip_CaptchaCode'] != $_SESSION['firstcaptcha'] ) {
  54. $flagvarx = 1;
  55. }
  56. }
  57. ?>
Lastly, I will show email programming to send something from a Corel Paradox application. Like the Windows Phone example, this requires an email client to be set up.
  1. method pushButton(var eventInfo Event)
  2. var
  3. ;// declare variables and objects.
  4. EmailCounter Smallint
  5. tblVar Table
  6. tc Tcursor
  7. m MAIL
  8. emailvar String
  9. endVar
  10. ;// change cursor to hour glass.
  11. setMouseShape(MouseWait,TRUE)
  12. ;// open a Tcursor to the data table containing the list of
  13. ;// email addresses to blast to.
  14. if tc.open(":PDOXDATA:EmailBlast.db") then
  15. ;// log onto the mail system.
  16. m.logon("MyPassword", "MyMailProfile")
  17. ;// start edit mode
  18. tc.edit()
  19. ;// move Tcursor to the first record in the data table.
  20. tc.home()
  21. ;// initialize the counter for the number of emails sent out.
  22. EmailCounter = 0
  23. ;// begin the “while loop” for traversing the data table to send out
  24. ;// emails for each address.
  25. while NOT tc.eot()
  26. ;// if the record isn’t marked as already processed,
  27. ;// go ahead and construct the email.
  28. if (tc."Finished" <> "y") then
  29. ;// add email address.
  30. m.addAddress(tc."E-mail")
  31. ;// add subject line.
  32. m.setSubject("An Email Blast")
  33. ;// add email message.
  34. m.setMessage("Here is an email blast we are sending out…success is expected…")
  35. ;// add attachment.
  36. m.addAttachment("c:\\document_folder\\sample.doc")
  37. ;// send email.
  38. m.send()
  39. ;// clear away addresses and attachments.
  40. m.emptyAddresses()
  41. m.emptyAttachments()
  42. ;// mark the just sent email as being processed in the data table.
  43. tc."Finished" = "y"
  44. ;// increment the sent email counter by 1.
  45. EmailCounter = EmailCounter + 1
  46. ;// end of if-then logic for unsent emails.
  47. endif
  48. ;// move to the next sequential record in the data table.
  49. tc.nextRecord()
  50. ;// temporarily suspend processing if 25 emails have been blasted out.
  51. ;// reset the “EmailCounter” variable to 0 and then display message to console.
  52. if (EmailCounter > 25) then
  53. EmailCounter = 0
  54. msgStop("Message", "Click to continue after these 25 emails went out...")
  55. endif
  56. ;// loop around again to process the next sequential email in the data table.
  57. endWhile
  58. ;// end edit mode
  59. tc.endEdit()
  60. ;// close a Tcursor to the data table containing the list of email addresses to blast to.
  61. tc.close()
  62. ;// log off the mail system.
  63. m.logoff()
  64. ;// end of if-then logic for opening a Tcursor to
  65. ;// ":PDOXDATA:EmailBlast.db" data table.
  66. endif
  67. ;// change cursor back to mouse pointer.
  68. setMouseShape(MouseArrow,TRUE)
  69. EndMethod
The three examples described here generally follow the same idea for programmatically constructing sent email. This is one of the most useful skills a developer can learn in today’s world. Whether it is needed for a smart phone app, web page or desktop application, it’s a skill one can't do without.