IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Tech
Search Tech Group:

Advanced
-Collapse +Expand Tech Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBComputer TechSoftware   Print This    All Groups  
 
Tech Software:
Three Ways to Program Email within Applications
 
Posted 13 months ago on 11/12/2020 and updated 12/13/2020
Take Away:

This article discusses three different coding language methods to orchestrate sent email from within applications.

KB104907

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# coding.  An email account needs to first be set up on the phone.

  1. // create email object.
  2. EmailComposeTask emailComposeTask = newEmailComposeTask();  
  3. // add a subject.
  4. Subject = "test subject";  
  5. // add a message.
  6. Body = "test message";  
  7. // add a recipient.
  8. To = "test_recipient@example.com";  
  9. // add a carbon copy recipient.
  10. Cc = "test_cc@example.com";  
  11. // add a blind carbon copy recipient.
  12. Bcc = "test_bcc@example.com";  
  13. // bring up the email interface.
  14. 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. session_start();    
  2. // include this php file to process the ‘captcha’ code to verify the   
  3. // sender is not a robot.  
  4. include("simple-php-captcha.php");    
  5. // initialize php error variables to check for valid fields of information.  
  6. $flagvar= 0;    
  7. $flagvar1= 0;    
  8. $flagvar2= 0;    
  9. $flagvar3= 0;    
  10. $flagvarx= 0;    
  11. // assign the auto generated captcha code to a  
  12. // php session variable used to display it to the email sender.  
  13. $_SESSION[‘captcha‘] = captcha();    
  14. // enter here after the user clicks the Submit button  
  15. // to send out the email.  
  16. if($_POST[‘submit_email‘]) {    
  17. // check the sender’s email address to make sure it is valid and set
  18. // error variables.  
  19. if(strlen($_POST[‘EMAIL‘]) <= 0) {    
  20. $flagvar1= 1;    
  21. $flagvar= 1;    
  22. elseif (!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. // if the 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(‘analyz.oh@netzero.net‘, $_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. // if the captcha codes do not match, set an error flag.  
  53. if( $_REQUEST[‘skip_CaptchaCode‘] != $_SESSION[‘firstcaptcha‘] ) {    
  54. $flagvarx= 1;    
  55. }    
  56. }    

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. ;// change cursor to hour glass.
  2. setMouseShape(MouseWait,TRUE)  
  3. ;// open a Tcursor to the data table containing the list of  
  4. ;// email addresses to blast to.  
  5. ifopen(":PDOXDATA:EmailBlast.db") then  
  6. ;// log onto the mail system.
  7. logon("MyPassword", "MyMailProfile")  
  8. ;// start edit mode
  9. edit()  
  10. ;// move Tcursor to the first record in the data table.
  11. home()  
  12. ;// initialize the counter for the number of emails sent out.
  13. EmailCounter = 0  
  14. ;// begin the while loop for traversing the data table to send out
  15. ;// emails for each address.
  16. whileNOT tc.eot()  
  17. ;// if the record isn’t marked as already processed,
  18. ;// go ahead and construct the email.
  19. if(tc."Finished" <> "y") then  
  20. ;// add email address.
  21. addAddress(tc."E-mail")  
  22. ;// add subject line.
  23. setSubject("An Email Blast")  
  24. ;// add email message.
  25. setMessage("Here is an email blast we are sending out…success is expected…")  
  26. ;// add attachment.
  27. addAttachment("c:\\document_folder\\sample.doc")  
  28. ;// send email.
  29. send()  
  30. ;// clear away addresses and attachments.
  31. emptyAddresses()  
  32. emptyAttachments()   
  33. ;// mark the just sent email as being processed in the data table.
  34. "Finished"= "y"  
  35. ;// increment the sent email counter by 1.
  36. EmailCounter = EmailCounter + 1  
  37. ;// end of if-then logic for unsent emails.
  38. endif
  39. ;// move to the next sequential record in the data table.
  40. nextRecord()             
  41. ;// temporarily suspend processing if 25 emails have been blasted out.
  42. ;// reset the “EmailCounter” variable to 0 and then display message
  43. ;// to console.
  44. if(EmailCounter > 25) then  
  45. EmailCounter = 0  
  46. msgStop("Message", "Click to continue after these 25 emails went out...")  
  47. endif
  48. ;// loop around again to process the next sequential email in the data table.
  49. endWhile          
  50. ;// end edit mode
  51. endEdit()              
  52. ;// close a Tcursor to the data table containing the list of email
  53. ;// addresses to blast to.
  54. close()                   
  55. ;// log off the mail system.
  56. logoff()  
  57. ;// end of if-then logic for opening a Tcursor to
  58. ;// ":PDOXDATA:EmailBlast.db" data table.
  59. endif
  60. ;// change cursor back to mouse pointer.
  61. setMouseShape(MouseArrow,TRUE)  

The three examples described here generally follow the same idea for programmatically constructing sent email. This is one of the most useful skills a software 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.


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P147A1
Enter key:
Article Contributed By NE Ohio Computer Guy:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues. And don‘t forget to check out my YouTube Channel. It is full of cool videos and has something for everyone!

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #104907 Counter
143
Since 11/12/2020
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2021 Prestwood IT Solutions.   [Security & Privacy]