April 7, 2020
Estimated Post Reading Time ~ 1 mins

AEM – Custom Template Email

Screenshot-2019-06-13-at-12.04.53-AM

AEM provides OOTB email templates to send emails for workflow notification, completion, etc.
But those templates are plain text and cannot provide rich UI and limited to few dynamic properties.
Example – OOTB workflow notification email template contains plain text and other variables like an event, work item, user, host properties, etc.

More info about Email Templates for Workflow Notification at https://helpx.adobe.com/experience-manager/6-3/sites/administering/using/notification.html#ConfiguringtheWorkflowEmailNotificationService
ootb
OOTB Workflow notification template

Custom Email

Custom Email Service/Servlet/Process Step:

You can create a utility/Servlet/workflow Process, whichever trigger the email and use com.day.cq.mailer.MessageGatewayService to send template based email.
Add Below dependencies in POM. or check dependencies using dependency finder at http://localhost:4504/system/console/depfinder
pom
Dependencies in POM
Create a utility class, In this class inject MessageGatewayService service using Reference annotation and Create a map with all the dynamic properties. e.g.
@Reference
private MessageGatewayService messageGatewayService;
final Map<String, String> parameters = new HashMap<String, String>();
parameters.put(“title”, “Demo Email”);

Create org.apache.commons.mail.HTML email class object with template and map parameters.
HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(parameters), HtmlEmail.class);
Find the below Servlet Example for complete code –
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/email/HTMLEmailServlet.java

Custom Email template.txt

You can create a custom HTML5 template like an HTML page and save it as .txt in CRX repository wherever you want.
All the variable values can be replaced with properties set in the code e.g. ${title}

Example template available at
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/email/html5-template.txt

OSGi Config to Send an email:

For AEM to be able to send emails, the Day CQ Mail Service needs to be properly configured. Please check at
https://helpx.adobe.com/experience-manager/6-3/sites/administering/using/notification.html#configmail

mail-service-osgi-config
Sample configuration for sending an email using Gmail SMTP server
That’s it.
Find the sample email trigger by https://github.com/arunpatidar02/aem63app-repo/blob/master/java/email/HTMLEmailServlet.java servlet.
htmlemail
Find the complete code used in this blog at Github.

Source: https://aem.adobemarketingclub.com/aem-custom-template-email/
aem4beginner.blogspot


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.

Ad Blocker Detected :(

Please consider supporting us by disabling your ad blocker.

Please Disable your adblocker and Refresh the page to view the site content.