Tuesday 30 December 2014

Send Mail with Multiple Attachments using PT_MCF_MAIL

Using an Application Engine, I want to attach multiple attachments to an outgoing email.

Combine both for Record PeopleCode usage.

Component array &UserFileArray, &SysFileArray;

&UserFileArray = CreateArray();
&SysFileArray = CreateArray();

&FileRS = CreateRowset(Record.MYRECORD);
&FileRS.Fill("WHERE OPRID = :1 AND RUN_CNTL_ID = :2", &Oprid, &RunControlID);

For &i = 1 To &FileRS.ActiveRowCount
   
 If Substring(GetEnv("PS_HOME"), 1, 1) = "/" Then
  &Slash = "/";
 Else
  &Slash = "\";
 End-If;
 
 &UserFileName = &FileRS(&i).GetRecord(1).ATTACHUSERFILE.Value;
 &SysFileName = &FileRS(&i).GetRecord(1).ATTACHSYSFILENAME.Value;
 &LocalFileName = &FilePath | &Slash | &SysFileName;

 &UserFileArray.Push(&UserFileName);
 &SysFileArray.Push(&LocalFileName);

 file processing code here
 
End-For;
import PT_MCF_MAIL:*;

Component array &UserFileArray, &SysFileArray;
Local array &MultipleFileArray;
Local PT_MCF_MAIL:MCFOutboundEmail &Email = create PT_MCF_MAIL:MCFOutboundEmail();
Local PT_MCF_MAIL:MCFBodyPart &Text = create PT_MCF_MAIL:MCFBodyPart();
Local PT_MCF_MAIL:MCFBodyPart &Attachment = create PT_MCF_MAIL:MCFBodyPart();
Local PT_MCF_MAIL:MCFMultipart &MP = create PT_MCF_MAIL:MCFMultipart();
Local integer &ReturnCode;

&Email.From = "sender@email.com;
&Email.Recipients = "recipient@email.com";
&Email.Subject = "Test Subject";

&Text.Text = "Test Body";
&MP.AddBodyPart(&Text);

&MultipleFileArray = CreateArray();
For &i = 1 To &SysFileArray.Len
   &Attachment = create PT_MCF_MAIL:MCFBodyPart();
   &MultipleFileArray [&i] = &Attachment;
   &MultipleFileArray [&i].SetAttachmentContent(&SysFileArray [&i], %FilePath_Absolute, &UserFileArray [&i], " ", "", "");
   &MP.AddBodyPart(&MultipleFileArray [&i]);
End-For;

&Email.MultiPart = ∓

&ReturnCode = &Email.Send();

Evaluate &ReturnCode
When %ObEmail_Delivered
   MessageBox(0, "", 0, 0, "Email Sent Successfully");
   Break;
When %ObEmail_NotDelivered
   MessageBox(0, "", 0, 0, "Email Not delivered" | &Email.InvalidAddresses | &Email.ValidSentAddresses | &Email.ValidUnsentAddresses);
   Break;
When %ObEmail_PartiallyDelivered
   MessageBox(0, "", 0, 0, "Email Partially delivered" | &Email.InvalidAddresses | &Email.ValidSentAddresses | &Email.ValidUnsentAddresses);
   Break;
When %ObEmail_FailedBeforeSending
   MessageBox(0, "", 0, 0, "Email Failed Before Sending" | &Email.ErrorDescription | &Email.ErrorDetails);
   Break;
End-Evaluate;

No comments:

Post a Comment