Custom Mail Template

So in this blog, we will see how we can customize the mail template and how we can use that template in the workflow model.

How to Customize the Mail Template?
First, we must go to this path /libs/settings/workflow/notification/email/default/en.txt and copy the en.txt file to the apps.


Then we will use /apps/settings/workflow/notification/email/default/workflow-completed.txt path in the Arguments field in the process step of workflow as shown below:

And we did this code for the process step to get the mail when the workflow was completed.

package com.demo.core.workflows;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.Workflow;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.commons.mail.MailTemplate;
import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;
import org.apache.commons.lang.text.StrLookup;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.HtmlEmail;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.commons.PropertiesUtil;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Component(immediate = true,
        service = WorkflowProcess.class,
        property = {
                Constants.SERVICE_DESCRIPTION + "=This is Demo Workflow",
                Constants.SERVICE_VENDOR + "=Adobe Systems",
                "process.label" + "= Process step for for demo"
        })

public class DemoProcessStep implements WorkflowProcess {

    @Reference
    private MessageGatewayService messageGatewayService;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {

        ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);
        UserManager userManager = resolver.adaptTo(UserManager.class);
        String payload = workItem.getWorkflowData().getPayload().toString();
        String initiator = workItem.getWorkflow().getInitiator();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();

        final Map<String, String> parameters = new HashMap<>();
        parameters.put("title", "Demo Email Workflow");
        parameters.put("participant.name", "Administrator");
        parameters.put("participant.id", initiator);
        parameters.put("host.prefix", "http://localhost:4502");
        parameters.put("event.TimeStamp", dtf.format(now));
        parameters.put("payload.path.open", payload);
        parameters.put("item.node.title", "No Information");

        String processArguments = null;
        Workflow workflow = workItem.getWorkflow();
        processArguments = metaDataMap.get("PROCESS_ARGS", "Default");
        Node templateNode = resolver.getResource(processArguments).adaptTo(Node.class);
        Authorizable authorizable = null;
        String userEmail = null;

        try {
            authorizable = userManager.getAuthorizable("aemmainuser");
        } catch (RepositoryException e) {
            e.printStackTrace();
        }

        try {
            userEmail = PropertiesUtil.toString(authorizable.getProperty("profile/email"), "");
        } catch (RepositoryException e) {
            e.printStackTrace();
        }

        try {
            MessageGateway<Email> messageGateway;

            final MailTemplate mailTemplate = MailTemplate.create(processArguments, templateNode.getSession());
            HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(parameters), HtmlEmail.class);

            String emailToRecipients = userEmail;
            String emailCcRecipients = "abc@gmail.com";
            email.addCc(emailCcRecipients);
            email.addTo(emailToRecipients);
            email.setSubject("Completed Workflow");
            email.setFrom("avnirajput2803@gmail.com");

            // Inject a MessageGateway Service and send the message
            messageGateway = messageGatewayService.getGateway(HtmlEmail.class);

            // Check the logs to see that messageGateway is not null
            messageGateway.send(email);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Here we got the mail after completing the workflow with the custom mail template we created.

Avni Rajput