Categories
PeopleCode

PeopleSoft MCF Email Class

Since PeopleSoft has two methodologies to send email, one of them is delivered Sendmail methods & another is Multichannel Framework in short form called – MCF Email class.

Since SendMail function has been deprecated and oracle strongly recommend not to use, hence it is good idea to use MCF email application class in your bolt on applications.Be frankly we still use SendMail methods specially on workflow peoplecode events if our sending business logic is too simple, for say we need to send just sending normal email.If fact MCF email classes also simple procedure to send email, but some time due to Application package complexity we bypass the MCF email classes and use SendMail functions.

This blog post i will share, a wrapper class of MCF Email that will send email with attachment in simple 5 lines of code.So lets start.

Wrapper in the sense, i have created another Class Called Sendmail that has four methods

Method NamePurpose
SendmailSend Email with To CC BCC & ReplyTo ( To & CC can Comma Separated )
SetAttachment Send Attachment with File Path & File Name
Compose Compose Email with Mail Subject & Mail body
Send Finally Send Email it will return delivery result code & status text

Methods Parameter Information

Method NameParameters
Sendmail &sFrom As string , &sTo As string , &sCC As string , &sBCC As string , &sReplyTo As string
SetAttachment&sFileAttachPath As string , &sFileAttachName As string
Compose &EmailSubjectLine As string , &EmailBodyLine As string
SendReturn Value EmailResult , EmailResultText

So, this is over all introduction of My Sendmail class , now i will discuses full descriptions of each part.

Full App Class Code

/** Wrapper Class of MCF_EMAIL; **/
/** Just Import the class, then Call Sendmail(), SetAttachment , Compose & Send **/
/** Use this Way in Any PeopleCode events
import DBT_PCLOG:*;
Local DBT_PCLOG:Sendmail &oEmail = create DBT_PCLOG:Sendmail("[email protected]", "[email protected],[email protected]", "[email protected],[email protected]", "", "[email protected]");
&oEmail.SetAttachment("D:\ChatExtension.pdf", "ChatExtension.pdf");
&oEmail.Compose("Hello World - " | %DbName, "body content" | %DbName);
&oEmail.Send();
MessageBox(0, "", 0, 0, "" | &oEmail.EmailResult);
MessageBox(0, "", 0, 0, "" | &oEmail.EmailResultText);
**/

/** Written 21-Oct-2014 **/
/** Ver - 1 **/

import PT_MCF_MAIL:*;

class Sendmail
   method Sendmail(&sFrom As string, &sTo As string, &sCC As string, &sBCC As string, &sReplyTo As string);
   method SetAttachment(&sFileAttachPath As string, &sFileAttachName As string);
   method Compose(&EmailSubjectLine As string, &EmailBodyLine As string);
   method Send();
   
   /* Email Addresses Settings */
   property string EmailFrom;
   property string EmailTo;
   property string EmailCC;
   property string EmailBCC;
   property string EmailReplyTo;
   
   /* Email message properties */
   property string EmailSubject;
   property string EmailText;
   
   /* Email Set Attachment File */
   property string FileAttachment;
   property string AttachFile;
   property string AttachFileTitle;
   
   /* Send Email Delivery result */
   property number EmailResult;
   property string EmailResultText;
   
private
   
   Constant &HTML_CONTENT_TYPE = "text/html";
   Constant &HTML_CHARSET = "UTF-8";
   method GetResultText(&nResultCd As number) Returns string;
   instance PT_MCF_MAIL:MCFOutboundEmail &oEmail;
   instance PT_MCF_MAIL:MCFBodyPart &oHtml;
   instance PT_MCF_MAIL:MCFMultipart &OMp;
   instance PT_MCF_MAIL:MCFBodyPart &oAttach;
   
end-class;

/** Sendmail works as contructor **/
method Sendmail
   /+ &sFrom as String, +/
   /+ &sTo as String, +/
   /+ &sCC as String, +/
   /+ &sBCC as String, +/
   /+ &sReplyTo as String +/
   
   /** Create MCFOutboundEmail class object  **/
   &oEmail = create PT_MCF_MAIL:MCFOutboundEmail();
   
   /** Create MCFBodyPart class object **/
   &oHtml = create PT_MCF_MAIL:MCFBodyPart();
   
   /**  Create MCFMultiPart class object  **/
   &OMp = create PT_MCF_MAIL:MCFMultipart();
   
   /** Crete MCFBodyPart class object **/
   &oAttach = create PT_MCF_MAIL:MCFBodyPart();
   
   &EmailFrom = LTrim(RTrim(&sFrom, " "), " ");
   &EmailTo = LTrim(RTrim(&sTo, " "), " ");
   &EmailCC = LTrim(RTrim(&sCC, " "), " ");
   &EmailBCC = LTrim(RTrim(&sBCC, " "), " ");
   &EmailReplyTo = LTrim(RTrim(&sReplyTo, " "), " ");
   
   &oEmail.Importance = "high"; /* Importance High */
   &oEmail.Priority = 1; /* Priority High */
   
   If %This.EmailFrom <> "" Then
      &oEmail.From = %This.EmailFrom;
   Else
      &oEmail.From = "[email protected]";
   End-If;
   
   &oEmail.Recipients = %This.EmailTo;
   
   If %This.EmailCC <> "" Then
      &oEmail.CC = %This.EmailCC;
   End-If;
   
   If %This.EmailBCC <> "" Then
      &oEmail.BCC = %This.EmailBCC;
   End-If;
   
   If %This.EmailReplyTo <> "" Then
      &oEmail.ReplyTo = %This.EmailReplyTo;
   End-If;
   
end-method;


/** Set Attachment Content **/
/** Please Note - By default File Path Settings is %FilePath_Absolute **/
method SetAttachment
   /+ &sFileAttachPath as String, +/
   /+ &sFileAttachName as String +/
   
   /* Do Some Validations */
   &AttachFile = LTrim(RTrim(&sFileAttachPath, " "), " ");
   &AttachFileTitle = LTrim(RTrim(&sFileAttachName, " "), " ");
   
   If %This.AttachFile <> "" And
         %This.AttachFileTitle <> "" Then
      &oAttach.SetAttachmentContent(&sFileAttachPath, %FilePath_Absolute, &sFileAttachName, &sFileAttachName, "", "");
      &oAttach.Disposition = "Attachment"; /** Set Content Disposition As Attachment **/
      &OMp.AddBodyPart(&oAttach);
   End-If;
   
end-method;

/** Compse Email with Subject & Body **/
method Compose
   /+ &EmailSubjectLine as String, +/
   /+ &EmailBodyLine as String +/
   
   &oHtml.AddHeader("Content-Type", &HTML_CONTENT_TYPE);
   &oHtml.ContentType = &HTML_CONTENT_TYPE;
   
   &oHtml.Charset = &HTML_CHARSET;
   
   %This.EmailSubject = &EmailSubjectLine;
   &oEmail.Subject = %This.EmailSubject;
   
   %This.EmailText = &EmailBodyLine;
   &oHtml.Text = %This.EmailText;
   
   /** Set the SubType property **/
   &OMp.SubType = "alternative; differences=Content-type";
   
   /*  Add the body parts created earlier to the multi part object  */
   &OMp.AddBodyPart(&oHtml);
   
   /* Set the multi part object to the Outbound email object  */
   &oEmail.MultiPart = &OMp;
   
end-method;


/** Send Email,it will return Result Code & Result Text **/
method Send
   
   %This.EmailResult = &oEmail.Send();
   %This.EmailResultText = %This.GetResultText(%This.EmailResult);
   
end-method;


/** Private Methods, Used for Email Delivery Status Message **/
method GetResultText
   /+ &nResultCd as Number +/
   /+ Returns String +/
   
   Evaluate &nResultCd
   When = %ObEmail_Delivered
      REM Return 1;
      Return "Email was delivered Successfully";
      Break;
   When = %ObEmail_NotDelivered
      REM Return 2;
      Return "Email delivery not attempted";
      Break;
   When = %ObEmail_PartiallyDelivered
      REM Return 3;
      Return "Email has only been partially delivered. Only some of the addresses in the list of addresses were delivered to";
      Break;
   When = %ObEmail_FailedBeforeSending
      REM Return 0;
      Return "Failed before sending, not delivered";
      Break;
   When = %ObEmail_SentButResultUnknown
      REM -1;
      Return "Email was Sent but delivery status is unknown";
      Break;
   When-Other
      Return "Unknown result, most likely not delivered. Please Contact your system administrator";
      Break;
   End-Evaluate;
   
end-method;

Use this Way in any PeopleCode Events

import DBT_PCLOG:*;
Local DBT_PCLOG:Sendmail &oEmail = create DBT_PCLOG:Sendmail("[email protected]", "[email protected],[email protected]", "[email protected],[email protected]", "", "[email protected]");
&oEmail.SetAttachment("D:\ChatExtension.pdf", "ChatExtension.pdf");
&oEmail.Compose("Hello World - " | %DbName, GetHTMLText(HTML.DBT_AWE_HTML_EMAIL));
&oEmail.Send();
MessageBox(0, "", 0, 0, "" | &oEmail.EmailResult);
MessageBox(0, "", 0, 0, "" | &oEmail.EmailResultText);

 Another Examples.

REM import class;
import DBT_PCLOG:*;
REM Create Object of Sendmail Class;
Local DBT_PCLOG:Sendmail &oEmail = create DBT_PCLOG:Sendmail("[email protected]", "[email protected],[email protected]", "[email protected],[email protected]", "", "[email protected]");
REM Set Attachment;
&oEmail.SetAttachment("D:\ChatExtension.pdf", "ChatExtension.pdf");
REM Compose Email with Subject & Body text;
&oEmail.Compose("Hello World - " | %DbName, "Mail Body Content ");
REM Send Email
&oEmail.Send();
MessageBox(0, "", 0, 0, "" | &oEmail.EmailResult);
MessageBox(0, "", 0, 0, "" | &oEmail.EmailResultText);

Download Full Code

Download the full App Class

Examples, How to create App Package with App Class

2014-10-21_00004
Create An App Package Call – DBT_PCLOG then insert an App Class Name with Sendmail

2014-10-21_00003

Heree is the Full App Class Chain.
Here is the Full App Class Chain.
2014-10-21_00005
Example Uses
With HTML Email
With HTML Email
HTML Content
HTML Content

Advantages of Wrapper Class

  • Out of box functionality
  • No Code overhead on page with app class
  • You can use this class for debugging purpose also.

Hope this helps 🙂

One reply on “PeopleSoft MCF Email Class”

Leave a Reply

Your email address will not be published. Required fields are marked *