Skip to content

Instantly share code, notes, and snippets.

@nvg
Created June 19, 2017 17:49
Show Gist options
  • Select an option

  • Save nvg/b8b17bee38088ed71e936bbb7b621b1e to your computer and use it in GitHub Desktop.

Select an option

Save nvg/b8b17bee38088ed71e936bbb7b621b1e to your computer and use it in GitHub Desktop.
peoplecode: email template
import PT_WF_NOTIFICATION:*;
class EmailTemplate
/*
Creates a new email template with the specified name. Email templates can be configured in
Main Menu > PeopleTools > Workflow > Notifications > Generic Templates
*/
method EmailTemplate(&templateNameParam As string);
method AddTo(&emailParam As string);
method AddCc(&emailParam As string);
method AddBcc(&emailParam As string);
/*
Binds parameter to the variable specified in the email template.
*/
method BindVar(&varParam As string);
method SetFromAddress(&fromAddressParam As string);
method SetReplyTo(&replyToParam As string);
method Send();
method SetHtml();
private
method ToAddress(&emailParam As string) Returns PT_WF_NOTIFICATION:NotificationAddress;
method GetContentType() Returns string;
instance boolean &isHtml;
instance string &templateName;
instance string &fromAddress;
instance string &replyTo;
instance array of string &aBindVariables;
instance array of PT_WF_NOTIFICATION:NotificationAddress &oNotifyTo;
instance array of PT_WF_NOTIFICATION:NotificationAddress &oNotifyCc;
instance array of PT_WF_NOTIFICATION:NotificationAddress &oNotifyBcc;
instance PT_WF_NOTIFICATION:NotificationAddress &oNotifyAddr;
instance PT_WF_NOTIFICATION:NotificationAddress &oNotifyCcAddr;
instance PT_WF_NOTIFICATION:NotificationAddress &oNotifyBccAddr;
instance PT_WF_NOTIFICATION:Notification &oNotif;
end-class;
method EmailTemplate
/+ &templateNameParam as String +/
&templateName = &templateNameParam;
&isHtml = False;
&aBindVariables = CreateArrayRept("", 0);
&oNotifyTo = CreateArrayRept(%This.ToAddress(""), 0);
&oNotifyCc = CreateArrayRept(%This.ToAddress(""), 0);
&oNotifyBcc = CreateArrayRept(%This.ToAddress(""), 0);
end-method;
method SetReplyTo
/+ &replyToParam as String +/
&replyTo = &replyToParam;
end-method;
method SetFromAddress
/+ &fromAddressParam as String +/
&fromAddress = &fromAddressParam;
end-method;
method Send
/* instantiate the NotificationTemplate class */
/* NotificationTemplate(component-name, component-market, Generic-template-name, G = Generic Teplate) */
Local PT_WF_NOTIFICATION:NotificationTemplate &oGenericTemplate = create PT_WF_NOTIFICATION:NotificationTemplate(%Component, %Market, &templateName, "G");
/* use the array of bind variables to populate the generic template */
Local string &xmlVars = &oGenericTemplate.SetupGenericVars(&aBindVariables);
Local boolean &alwaysTrue = &oGenericTemplate.GetAndExpandTemplate(%Language, &xmlVars);
/* instantiate the Notification class */
/* Notification(Notify-from-email-id, date-time, language) */
&oNotif = create PT_WF_NOTIFICATION:Notification(&fromAddress, %Date + %PerfTime, %Language);
/* set properties */
&oNotif.ContentType = %This.GetContentType();
&oNotif.NotifyTo = &oNotifyTo;
&oNotif.NotifyBCC = &oNotifyBcc;
&oNotif.EmailReplyTo = &replyTo; /* see note 1 */
&oNotif.Subject = &oGenericTemplate.Subject;
&oNotif.Message = &oGenericTemplate.Text; /* see note 2 */
/* send email */
&oNotif.Send();
end-method;
method AddTo
/+ &emailParam as String +/
&oNotifyTo.Push(%This.ToAddress(&emailParam));
end-method;
method AddCc
/+ &emailParam as String +/
&oNotifyCc.Push(%This.ToAddress(&emailParam));
end-method;
method AddBcc
/+ &emailParam as String +/
&oNotifyBcc.Push(%This.ToAddress(&emailParam));
end-method;
method ToAddress
/+ &emailParam as String +/
/+ Returns PT_WF_NOTIFICATION:NotificationAddress +/
Return (create PT_WF_NOTIFICATION:NotificationAddress("", "", "", &emailParam, "Email"));
end-method;
method BindVar
/+ &varParam as String +/
&aBindVariables.Push(&varParam);
end-method;
method GetContentType
/+ Returns String +/
If &isHtml Then
Return "Content-type: text/html; charset=UTF8";
Else
Return "Content-type: text/plain; charset=UTF8";
End-If;
end-method;
method SetHtml
&isHtml = True;
end-method;
@tsasgar
Copy link
Copy Markdown

tsasgar commented Jul 31, 2020

Good job there. Can you add file attachments for email using FileNames and FileTitles properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment