Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to use custom email template in a workflow step?

Avatar

Level 7

Hi Team,

We have the default email notification that is used in workflow at /etc/workflow/notification/email/default/en.txt location. I have created my own email notification template at /etc/workflow/notification/email/myproject/en.txt. I'm using "Dynamic Participant Step" in my workflow and checked "Email" checkbox in order to send the email notification. It is using the default email template. Where can I specify my custom template to use in workflow?

Please suggest.

Thanks,

AryA.

1 Accepted Solution

Avatar

Correct answer by
Level 10

We are writing a HELPX article to show how to use the MessageServiceGateWay API to send emails based on a template. And how to populate it via a MAP object that contains key/values. But as i stated in my first response - to use a custom EMail template in a workflow - you need to write a custom WORKFLOW Step then Java such as:

  //Sends HTML email based on an AEM Template
     
    private void SendHHTMLEmail(SlingHttpServletRequest request)
    {
         try
         {
              Map myMap = new HashMap() ; 
              
              myMap.put("host.prefix","Foo"); //use a map entry for each variable to populate
             
             //Declare a MessageGateway service
             MessageGateway<HtmlEmail> messageGateway; 

            
             //Specify the EMail template to use in email
             String template ="/etc/notification/email/html/com.day.cq.collab.forum/en.txt";
             
            Resource templateRsrc = request.getResourceResolver().getResource(template);
            
            String path = templateRsrc.getPath(); 
             
            
            
            MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), templateRsrc.getResourceResolver().adaptTo(Session.class));
                     
           HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(myMap), HtmlEmail.class);
            
              email.addTo("scottm@adobe.com");
           email.setSubject("AEM Email Template");
           email.setFrom("smacdonald2006@hotmail.com"); 
          
              
            
          //Inject a MessageGateway Service and send the message
           
            
          //  messageGateway = messageGatewayService.getGateway(Email.class);
            messageGateway = this.messageGatewayService.getGateway(HtmlEmail.class);
          
            
            // gateway.send(email);
             
           
         // Check the logs to see that messageGateway is not null
            messageGateway.send((HtmlEmail) email);
           // messageGateway.send((Email) email);
         }
           
             catch (Exception e)
             {
                 
                  log.info(e.getMessage())  ; 
             }
    }
     

View solution in original post

4 Replies

Avatar

Level 10

To use a custom template in a Workflow step - write a custom step and use Java located here: 

https://forums.adobe.com/thread/1107034

We have this almost in a community article: 

http://scottsdigitalcommunity.blogspot.ca/2013/10/creating-custom-aem-workflow-steps-that.html

The only difference is we are not using a email template - so you would add the logic in the 1st link to the custom step. 

Avatar

Level 10

Also - read Dan Klco post here - he does a good job explaining how to get a custom template using Java code: 

http://labs.6dglobal.com/blog/2012-08-20/sending-email-adobe-cq-api/

Avatar

Correct answer by
Level 10

We are writing a HELPX article to show how to use the MessageServiceGateWay API to send emails based on a template. And how to populate it via a MAP object that contains key/values. But as i stated in my first response - to use a custom EMail template in a workflow - you need to write a custom WORKFLOW Step then Java such as:

  //Sends HTML email based on an AEM Template
     
    private void SendHHTMLEmail(SlingHttpServletRequest request)
    {
         try
         {
              Map myMap = new HashMap() ; 
              
              myMap.put("host.prefix","Foo"); //use a map entry for each variable to populate
             
             //Declare a MessageGateway service
             MessageGateway<HtmlEmail> messageGateway; 

            
             //Specify the EMail template to use in email
             String template ="/etc/notification/email/html/com.day.cq.collab.forum/en.txt";
             
            Resource templateRsrc = request.getResourceResolver().getResource(template);
            
            String path = templateRsrc.getPath(); 
             
            
            
            MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), templateRsrc.getResourceResolver().adaptTo(Session.class));
                     
           HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(myMap), HtmlEmail.class);
            
              email.addTo("scottm@adobe.com");
           email.setSubject("AEM Email Template");
           email.setFrom("smacdonald2006@hotmail.com"); 
          
              
            
          //Inject a MessageGateway Service and send the message
           
            
          //  messageGateway = messageGatewayService.getGateway(Email.class);
            messageGateway = this.messageGatewayService.getGateway(HtmlEmail.class);
          
            
            // gateway.send(email);
             
           
         // Check the logs to see that messageGateway is not null
            messageGateway.send((HtmlEmail) email);
           // messageGateway.send((Email) email);
         }
           
             catch (Exception e)
             {
                 
                  log.info(e.getMessage())  ; 
             }
    }
     

Avatar

Level 7

Thanks Scott. Thanks for your help in offline too.