Search This Blog

Wednesday, September 16, 2009

Email Examples using PeopleCode

Contents:
<![if !supportLists]>·         <![endif]>Creating Test mail
<![if !supportLists]>·         <![endif]>Creating Email and Overriding SMTP Settings
<![if !supportLists]>·         <![endif]>Creating HTML Mail
<![if !supportLists]>·         <![endif]>Creating Multi-Part Email With Both Text and HTML Parts
<![if !supportLists]>·         <![endif]>Creating HTML Email with Images
<![if !supportLists]>·         <![endif]>Creating e-mails with Attachments
<![if !supportLists]>·         <![endif]>Creating Email Attachments Specifying a URL
<![if !supportLists]>·         <![endif]>Creating Multiple Emails
<![if !supportLists]>·         <![endif]>Authenticating Email While Sending

The following are some examples of how to create the most common types of email.

Creating Test mail

The following code example creates and sends a very simple email, then tests the results.
import PT_MCF_MAIL:*;

/*-- Create an outbound email object --*/
  
Local PT_MCF_MAIL:MCFOutboundEmail &eMail =
create PT_MCF_MAIL:MCFOutboundEmail();
  
/*-- Initialize the usual fields of an email --*/
  
&eMail.From = &FromAddress;
&eMail.Recipients = &ToList;
&eMail.Subject = &Subject;
&eMail.Text = &MailBody;
  
/*-- The send method uses the default SMTP parameters as set in the app server configuration file.

This send method makes a connection to the SMTP server, sends the mail and then disconnects.

The result are returned as a number corresponding to the possible values.

The list of ValidSent, InvalidSent and Invalid addresses are returned in the email object itself
----*/
  
Local integer &resp = &eMail.Send();
Local boolean &done;
  
Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
      Break;
     
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
      Break;
     
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
      Break;
     
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
&email.ErrorDetails;*/
      
      &done = False;
      Break;
   End-Evaluate;

Creating Email and Overriding SMTP Settings

The following code example creates a text email and overrides the SMTP settings as found in the application server configuration file.
import PT_MCF_MAIL:*;

/*-- Create an email object by setting individual parameters ---*/

Local PT_MCF_MAIL:MCFOutboundEmail &eMail =
create PT_MCF_MAIL:MCFOutboundEmail();

&eMail.Recipients = &ToList; /* comma separated list of email addresses */
&eMail.CC = &CCList; /* comma separated list of email addresses */
&eMail.BCC = &BCCList; /* comma separated list of email addresses */
&eMail.From = &FromAddress; /* from email address */
&eMail.ReplyTo = &ReplyToAddress; /* in case the reply is to be sent to a different email address */
&eMail.Sender = &SenderAddress; /* If different from the "from" address */
  
&eMail.Subject = &Subject; /* email subject line */
&eMail.Text = &MailBody; /* email body text */
  
  
/*-- Override the default SMTP parameters specified in app server configuration file ----*/
  
&eMail.SMTPServer = "psp-smtpg-01";
&eMail.SMTPPort = 10266; /*-- Usually this is 25 by default */
  
Local integer &resp = &eMail.Send();
 
/* Now check the &resp for the result */

Local boolean &done;
  
Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
      Break;
     
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
      Break;
     
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
      Break;
     
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
&email.ErrorDetails;*/
     
      &done = False;
      Break;
   End-Evaluate;

Creating HTML Mail

The following example creates an HTML email.
import PT_MCF_MAIL:*;

/*-- Create an email object by setting individual parameters
---*/


Local PT_MCF_MAIL:MCFOutboundEmail &email =
create PT_MCF_MAIL:MCFOutboundEmail();
  
   &email.From = &FromAddress;
   &email.Recipients = &ToList;
   &email.Subject = &Subject;
  
&email.Text =
"<html><body><H1><b>Hi there!</b></H1><P>We are ready.</body></html>";

   &email.ContentType = "text/html";
  
Local integer &res = &email.Send();

Local boolean &done;
  
Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
      Break;
     
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
      Break;
     
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
      Break;
     
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
&email.ErrorDetails;*/
     
      &done = False;
      Break;
   End-Evaluate;


Creating Multi-Part Email With Both Text and HTML Parts

The following code example creates an email with both HTML and text sections.
The email client on the target host must be able to detect the HTML and text parts in the email and display the part that the client is configured to display.
import PT_MCF_MAIL:*;

Local PT_MCF_MAIL:MCFOutboundEmail &email =
create PT_MCF_MAIL:MCFOutboundEmail();
Local string &TestName = "Text and its alternate html body";
  
&email.From = &FromAddress;
&email.Recipients = &ToList;
&email.Subject = &Subject;
  
Local PT_MCF_MAIL:MCFBodyPart &text = create PT_MCF_MAIL:MCFBodyPart();
&text.Text = "Hi There";
  
Local PT_MCF_MAIL:MCFBodyPart &html = create PT_MCF_MAIL:MCFBodyPart();
&html.Text =
"<html><BODY><H1>EMail test with HTML content</H1><b>Hi There</b>" |
"<A href='http://www.peoplesoft.com'>Check this out!</A>" |
"<P></BODY></html>";
&html.ContentType = "text/html";
  
Local PT_MCF_MAIL:MCFMultipart &mp = create PT_MCF_MAIL:MCFMultipart();
&mp.SubType = "alternative; differences=Content-type";
  
&mp.AddBodyPart(&text);
&mp.AddBodyPart(&html);
  
&email.MultiPart = &mp;
  
Local integer &res = &email.Send();

Local boolean &done;
  
Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
      Break;
     
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
      Break;
     
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
      Break;
     
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
&email.ErrorDetails;*/
     
      &done = False;
      Break;
   End-Evaluate;


Creating HTML Email with Images

The following code example creates an HTML email with imbedded images. The content ID for an image can be a reference to a part in the email. In the following code example, the images become a part of the email and are transmitted as attachments to the email.
import PT_MCF_MAIL:*;

Local PT_MCF_MAIL:MCFOutboundEmail &email =
create PT_MCF_MAIL:MCFOutboundEmail();
 
   &email.From = &FromAddress;
   &email.Recipients = &ToList;
   &email.Subject = &Subject;
  

Local string &htmlText =
"<html>" |
"  <head><title></title></head>" |
"  <body>" |
"    <b>A sample jpg</b><br>" |
"    <IMG SRC=cid:23abc@pc27 width=566 height=424><br>" |
"    <b>End of jpg</b>" |
"  </body>" |
"</html>";

/* In this sample, the htmlText is assembled using | operator
only for the readability.
Specifying just one string can be more efficient
*/
  
   Local PT_MCF_MAIL:MCFMultipart &mp = create PT_MCF_MAIL:MCFMultipart();
   &mp.SubType = "related";
  
   Local PT_MCF_MAIL:MCFBodyPart &html = create PT_MCF_MAIL:MCFBodyPart();
   &html.Text = &htmlText;
   &html.ContentType = "text/html";
  
   Local PT_MCF_MAIL:MCFBodyPart &image = create PT_MCF_MAIL:MCFBodyPart();
  
&image.SetAttachmentContent("///file:C:/User/Documentum/XML%20Applications/proddoc/peoplebook_upc/peoplebook_upc.dtd",
 %FilePath_Absolute, "sample.jpg", "This is a sample image!", "", "");
  
   &image.AddHeader("Content-ID", "23abc@pc27");
  
   &mp.AddBodyPart(&html);
   &mp.AddBodyPart(&image);
  
   &email.MultiPart = &mp;
  
   Local integer &res = &email.Send();

Local boolean &done;
  
Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
      Break;
     
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
      Break;
     
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
      Break;
     
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
&email.ErrorDetails;*/
     
      &done = False;
      Break;
   End-Evaluate;

 

Creating e-mails with Attachments

The following code example creates an email with an attachment.
PeopleSoft recommends that you always provide the proper extension in the file name, otherwise the receiving email client may not be able to associate it with the appropriate application.
import PT_MCF_MAIL:*;

Local PT_MCF_MAIL:MCFOutboundEmail &email =
create PT_MCF_MAIL:MCFOutboundEmail();
  
&email.From = &FromAddress;
&email.Recipients = &ToList;
&email.Subject = &Subject;
  
Local string &plain_text = "Hi there!";
Local PT_MCF_MAIL:MCFBodyPart &text = create PT_MCF_MAIL:MCFBodyPart();
&text.Text = &plain_text;

Local PT_MCF_MAIL:MCFBodyPart &attach1 = create PT_MCF_MAIL:MCFBodyPart();
&attach1.SetAttachmentContent("Ocean Wave.jpg", %FilePath_Relative,
"Ocean Wave.jpg", "Ocean Wave", "", "");
/* %FilePath_Relative indicates the file is available at Appserver's FILES dierctory */
  
Local PT_MCF_MAIL:MCFBodyPart &attach2 = create PT_MCF_MAIL:MCFBodyPart();
&attach2.SetAttachmentContent("///file:C:/User/Documentum/XML%20Applications/proddoc/peoplebook_upc/peoplebook_upc.dtd",
 %FilePath_Absolute, "Sample.jpg", "Sample", "", "");
/* The Sample.jpg is available in the "public" folder of my-server machine*/
  
  
Local PT_MCF_MAIL:MCFMultipart &mp = create PT_MCF_MAIL:MCFMultipart();
&mp.AddBodyPart(&text);
&mp.AddBodyPart(&attach1);
&mp.AddBodyPart(&attach2);
  
&email.MultiPart = &mp;
  
Local integer &res = &email.Send();

Local boolean &done;
  
Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
      Break;
     
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
      Break;
      
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
      Break;
     
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
&email.ErrorDetails;*/
     
      &done = False;
      Break;
   End-Evaluate;

Creating Email Attachments Specifying a URL

The following code example creates an email attachment specifying a URL for the file location, instead of an absolute or relative path to the file.
Local PT_MCF_MAIL:MCFOutboundEmail &email =
create PT_MCF_MAIL:MCFOutboundEmail();
  
   &email.From = &FromAddress;
   &email.Recipients = &ToList;
   &email.Subject = &Subject;
  
   Local string &plain_text = "Hi there!";
   Local PT_MCF_MAIL:MCFBodyPart &text = create PT_MCF_MAIL:MCFBodyPart();
  
   &text.Text = &plain_text;
  
Local PT_MCF_MAIL:MCFBodyPart &attach1 = create PT_MCF_MAIL:MCFBodyPart();

&attach1.SetAttachmentContent("http://www.yahoo.com/members_agreement",
 %FilePath_Absolute, "hotmail.htm", "Hotmail Home page", "", "");
  
Local PT_MCF_MAIL:MCFBodyPart &attach2 = create PT_MCF_MAIL:MCFBodyPart();

&attach2.SetAttachmentContent("ftp://www.w3c/docs/somedoc",
 %FilePath_Absolute, "somedoc.htm", "somedoc from w3c", "", "");
  
   Local PT_MCF_MAIL:MCFMultipart &mp = create PT_MCF_MAIL:MCFMultipart();
   &mp.AddBodyPart(&text);
   &mp.AddBodyPart(&attach1);
   &mp.AddBodyPart(&attach2);
  
   &email.MultiPart = &mp;
  
Local integer &res = &email.Send();

Local boolean &done;
  
Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
      Break;
     
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
      Break;
     
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
      Break;
     
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
&email.ErrorDetails;*/
     
      &done = False;
      Break;
   End-Evaluate;

Creating Multiple Emails

You can create several emails and send them all in a single connection to the SMTP server. However, your system administrator may also need to configure the SMTP server with longer connection time-outs if you are sending multiple emails.
import PT_MCF_MAIL:*;

/*-- Create an email object by setting individual parameters
---*/
 Local array of PT_MCF_MAIL:MCFOutboundEmail &mails;
 Local PT_MCF_MAIL:MCFOutboundEmail &email;
  
 Local PT_MCF_MAIL:SMTPSession &commonSession =
create PT_MCF_MAIL:SMTPSession();
  
   &email = &commonSession.CreateOutboundEmail();
   &email.From = &FromAddress;
   &email.Recipients = &ToList1;
   &email.Subject = &Subject;
   &email.Text = &MailBody1;
  
   &mails = CreateArray(&email);
  
   &email = &commonSession.CreateOutboundEmail();
   &email.From = &FromAddress;
   &email.Recipients = &ToList2;
   &email.Subject = &Subject;
   &email.Text = &MailBody2;
   &mails [2] = &email;
  
   &email = &commonSession.CreateOutboundEmail();
   &email.From = &FromAddress;
   &email.Recipients = &ToList3;
   &email.Subject = &Subject;
   &email.Text = &MailBody3;
   &mails [3] = &email;
  
   Local array of integer &allRes = &commonSession.SendAll(&mails);

Authenticating Email While Sending

The following code example includes a user name and password to be used by the SMTP server.
Local PT_MCF_MAIL:MCFOutboundEmail &email =
create PT_MCF_MAIL:MCFOutboundEmail();
  
   &email.From = &FromAddress;
   &email.Recipients = &ToList;
   &email.Subject = &Subject;
   &email.Text = &MailBody;
  
   &email.SMTPServer = "MySMTPServer";
   &email.IsAuthenticationReqd = True;
  
   &email.SMTPUserName = "SomeLoginName";
   &email.SMTPUserPassword = "SomeLoginPassword";

   Local integer &res = &email.Send();



1 comment:

  1. I have been using the "Creating multiple emails" section to batch one of our longer running email processes and it worked great in out testing environments but now that we are in Production we are getting some java errors. I believe that it has to do with processing a larger set of email 3000+. Can you elaborate on the "your system administrator may also need to configure the SMTP server with longer connection time-outs if you are sending multiple emails?" I have been looking through the PeopleBooks SMTP settings and I cant seem to find anything that is helping. Thanks!

    ReplyDelete