I’d earlier written about sending HTML emails from PeopleCode here and more recently here. In both these posts, the email text was hardcoded thereby making it not possible for the business folks to edit.
In this post, however, I will show you how to use the Notification classes along with Generic Templates to send out an HTML email.
Why generic template?
By using Generic Templates, you can empower your business users to edit the wording of these emails without having to deal with the IT staff.
Generic Templates also offers multi-language support.
Setting up generic templates
Let’s start by setting up a Generic Template.
Navigation: PeopleTools > Workflow > Notifications > Generic Templates
As shown in the image, this is a simple page to use. You can use an online wysiwyg editor to generate the html if you are working on complex formatting.
If multi-language emails are required, you need to fill this page by selecting the desired language from the language selector.
You can add as many bind variables as you need and use them within the message text. You will have to use an array to pass the values of these bind variables as you will see in the code below.
App Package
You can use the PT_WF_NOTIFICATION package in this scenario. This package too uses the MCFOutboundEmail class as seen in the previous post to send out emails. However, unlike the MCFOutboundEmail, here we can use NotificationTemplate class to specify a Generic Template.
Sending Email – PeopleCode
/* import the package */ import PT_WF_NOTIFICATION:*; Local array of string &aBindVariables; Local array of PT_WF_NOTIFICATION:NotificationAddress &oNotifyTo; Local PT_WF_NOTIFICATION:NotificationAddress &oNotifyAddr; Local PT_WF_NOTIFICATION:Notification &oNotif; /* Create an array of to-email ids for the Notification class */ &oNotifyTo = CreateArrayRept(&oNotifyAddr, 0); &sToEmail = "xxxxxxxx@gmail.com"; /* NotificationAddress(Oprid, Description, Language, to-email-id, Channel) */ &oNotifyAddr = create PT_WF_NOTIFICATION:NotificationAddress("", "", "", &sToEmail, "Email"); &oNotifyTo.Push(&oNotifyAddr); /* instantiate the NotificationTemplate class */ /* NotificationTemplate(component-name, component-market, Generic-template-name, G = Generic Teplate) */ &oGenericTemplate = create PT_WF_NOTIFICATION:NotificationTemplate("", "", "R9_EMAIL_TEST", "G"); /* create an array of all bind variables */ &aBindVariables = CreateArrayRept("", 0); &aBindVariables.Push("John Smith"); &aBindVariables.Push("Rakesh"); /* use the array of bind variables to populate the generic template */ &xmlVars = &oGenericTemplate.SetupGenericVars(&aBindVariables); &oGenericTemplate.GetAndExpandTemplate(%Language, &xmlVars); /* instantiate the Notification class */ /* Notification(Notify-from-email-id, date-time, language) */ &oNotif = create PT_WF_NOTIFICATION:Notification("ps@psoftsearch.com", %Date + %PerfTime, %Language); /* set properties */ &oNotif.ContentType = "Content-type: text/html; charset=US-ASCII"; &oNotif.NotifyTo = &oNotifyTo; &oNotif.EmailReplyTo = "ps@psoftsearch.com"; /* see note 1 */ &oNotif.Subject = &oGenericTemplate.Subject; &oNotif.Message = &oGenericTemplate.Text; /* see note 2 */ /* send email */ &oNotif.Send(); |
Notes
1. I was not able to get the NotifyFrom to work correctly because of what seems to be a bug in the Notification class where it uses the email from PSUSEREMAIL if &NotifyFrom is NOT EMPTY (really?) and EmailReplyTo is empty.
So, if you set the EmailReplyTo property, the class uses that email id as the sender else it uses the user’s email id from PSUSEREMAIL as the sender. I’m on PT 8.54.08.
2. If you want a dynamic table or something similar, you can generate it as shown here and append it to the Message property.
Output
This is the email that the code sends out.
Conclusion
Using Generic template is a helpful way to give your user the ability to customise email text. PT_WF_NOTIFICATION is the app package that contains all the classes you need to do this. This method also provides muti-language support. As Generic templates are not managed objects, you will need to write Data Mover Scripts to copy these across to other instances.
Over to you!
Have you used generic templates to send emails? Were business users happy about the fact that they could customise the email text?
Hello SIr,
Very well explained. Email notifications using above code is working perfect for me, however, I am getting emails garbled for the special characters, even though I have used &mynotification.ContentType = “Content-type: text/html; charset=UTF-8”;
Can you please help with it ?
hi rahul, did you solve your problem? i have the same problem in my environment.
Dear Team,
Thanks for the article. We are trying to send a High Priority and Confidential email via templates. The mailbox used is Microsoft Outlook. Is there anyway to do this?
Thanks.
Hi Sasti,
Have a look at the MCFOutboundEmail Class Properties.
The Priority property and the Sensitivity property are the ones you are after.
I’ve covered MCFOutboundEmail class here.
Regards
Rakesh
Hi Rakesh,
Do you have code to send an attachment along with this generic template?
Thanks,
Ram.
Hi Ram,
You can use the FileNames and FileTitles properties for sending attachments – both accept arrays.
FileNames will have the absolute path of the file(s) to be attached.
FileTitles will have the name(s) with which these attachment(s) would be shown in the email.
Please add the following code before &oNotif.Send().
Cheers
Rakesh
Hi Rakesh,
Is there a way to embed an image in the mail body through this notification classes.
Regards,
Siva
@Siva, the easiest way is to use an img tag within the HTML to display a publicly accessible image.
Hi,
When we use the .Send method, how do we know that the email is sent. Because i tried the same putting this code in an app package, but the mail was not sent. Do we have an error handling for this
@Sai, You may want to have a look at this post which explains MCF classes to do this.
Hi,
How do we send a report kind of table in the body of email
Hi Prasant,
Were you able to achieve sending table within the body of email? could you share ho did you do this?
thanks
Manu
@Manu
This post shows how to send a table in the body.
Hi,
I am facing performance issue with Mail template.As it takes almost a min to send mail and save transaction.However,When i have tried without mail code transaction saved with in a sec only.
Thanks
Charu
Hi, Is there a way to assign email ids to BCC and CC?
@Sanjit
You can use the following properties for this.
Hi,
Is there a Way to trigger Generic Template HTML Emails in AWE?
Hi,
I am sending an attachment along with the generic template.
Is there a way to validate whether the mail is triggered or not.
as &Notify.Send() is not writing any returncode. Thanks
Hello Rakesh. thanks for the helpful information! I have a more rudimentary question and then a request:
1. So, let’s say I am wanting to add a template variable to the Generic Template called “Journal Denial”. Could you direct me to a resource online that helps me know where to edit the existing PeopleCode so that I could send the email with the new variable included as you have described in this article?
2. The link for the additional methods found in the connected article https://www.psoftsearch.com/html-email-using-mcf-class/ is broken. Could you please remedy this?
Thank you!
Scott