Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Send Content Approval email notification using Day CQ Mail service in Workflow Participant step

Avatar

Level 4

There is custom workflow to approve content created by author. We have Content Authors and Content Approver groups. We have used Participant step to approve content and checked checkbox to send email notification to the user group selected in thatParticipant step. We have SMTP credentials provided but not sure how to send email notifications using Day CQ Mail Service. I have added SMTP details in Day CQ Mail Service OSGI config. Do I need to write custom Participant or process step to send email notification to different users using SMTP credentials?

1 Accepted Solution

Avatar

Correct answer by
Level 4

HI @supriya-hande 

Basically, you have to write one process step that will be responsible for sending mail . that process step u have to configure in the workflow model.

please find the sample code to send mail

 

  (service = WorkflowProcess.class, property = { "process.label=NotificationMail" })

    public class NotificationMail implements WorkflowProcess {

        
        private MessageGatewayService messageGatewayService;

        public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {

            ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
            String path = item.getWorkflowData().getPayload().toString();
            Resource resource = resourceResolver.getResource(path.concat("/jcr:content"));
            ValueMap properties = resource.adaptTo(ValueMap.class);
            String title = StringUtils.EMPTY;
            String comment = StringUtils.EMPTY;
            String userId = StringUtils.EMPTY;
            if (properties.containsKey(JCRTITLE)) {
                title = properties.get(JCRTITLE, String.class) + ".html";
                LOGGER.info("jcrtitle+++++++++++++++++++++++++++++++++++++++++++" + title);
            }
            if (properties.containsKey(CQNAME)) {
                title = properties.get(CQNAME, String.class);
                LOGGER.info("cq:name+++++++++++++++++++++++++++++++++++++++++++" + title);
            }

            // Workflow wf = item.getWorkflow();
            List<HistoryItem> wfHistory = workflowSession.getHistory(item.getWorkflow());

            for (HistoryItem itemhistory : wfHistory) {
                LOGGER.info("history iem+++++++++++++++++++++++++++++++++++++++++++" + itemhistory.getUserId());
                LOGGER.info("comment+++++++++++++++++++++++++++++++++++++++++++" + itemhistory.getComment());
                comment = itemhistory.getComment();
                userId = itemhistory.getUserId();

            }

            LOGGER.info("path+++++++++++++++++++++++++++++++++++++++++++      " + path);

            String owner = item.getWorkflow().getInitiator();
            LOGGER.info("owner+++++++++++++++++++++++++++++++++++++++++++      " + owner);

            Date startdate = item.getTimeStarted();
            SimpleDateFormat mdyFormat = new SimpleDateFormat(REQDATE);
            String date = mdyFormat.format(startdate.getTime());

            LOGGER.info("startdate+++++++++++++++++++++++++++++++++++++++++++      " + date);

            try {
                // Declare a MessageGateway service
                MessageGateway<Email> messageGateway;

                // Set up the Email message
                HtmlEmail email = new HtmlEmail();
                // Set the mail values
                String emailToRecipients = "Raushan.jaiswal@gmail.com";
                String salutation = "<b>Dear  " + owner + ",</b><br>";
                String data = "<p><br><b>WorkArea:</b>\t" + path + "\t <b>Owner:\t" + userId + "\t <b>Intiated:</b>\t"
                        + date + "</p>\n";
                String message = "<b><p>" + owner + "</b> <b>has submitted content for your approval. "
                        + "Please review at your earliest convenience. After you review the content, click the Approve/Reject button below to Approve/Reject"
                        + ". If you feel that you are not the right person for approving the content, you can<a href=\"/notifications.html\"> reassign </a>this task. <b><br><br><a href= \"/notifications.html\">Approve/Reject</a>";

                comment = "<b>Description and Comments :<br>Description :\t Publishes content to one or more target nodes, optionally including a review / revision loop. "
                        + userId + date + "<br>Notes to Approver :\t" + comment
                        + "<br>Current Task: Please review and take action";
                String file = "<p><br><b>Attached Files:</b>\t</br><br>" + "<br>File Name :\t" + "<a href=\"/" + path
                        + ".html\">" + title + " </a>" + "       \t <b>Title:\t" + title + "</p>\n";

                email.addTo(emailToRecipients);
                // email.addCc(emailCcRecipients);
                email.setSubject("Please review and take action ");
                email.setFrom("hiraushan123@gmail.com");
                email.setMsg(salutation + message + data + comment + file);
                // 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) email);
                LOGGER.info("sendamil succesfully+++++++++++++++++++++");

 

 

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @supriya-hande 

Day CQ Mail Service is responsible for the configurations of the mail server that means which service will going to be used while sending email. Not responsible to send email.

You need to send email programmatically using the Day CQ Mail Service configurations.

Avatar

Level 4

Thanks @AsifChowdhury  for quick reply

 

Do you have any references on how to use Day CQ Mail Service configurations to send email notifications in workflow step?

Avatar

Community Advisor

@supriya-hande 

 

It can be configured with OOTB features available in AEM but you have to configure SMTP details in Day CQ Mail Service.

There is one similar thread https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/email-notifications-in-wor....

 

 

Avatar

Correct answer by
Level 4

HI @supriya-hande 

Basically, you have to write one process step that will be responsible for sending mail . that process step u have to configure in the workflow model.

please find the sample code to send mail

 

  (service = WorkflowProcess.class, property = { "process.label=NotificationMail" })

    public class NotificationMail implements WorkflowProcess {

        
        private MessageGatewayService messageGatewayService;

        public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {

            ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
            String path = item.getWorkflowData().getPayload().toString();
            Resource resource = resourceResolver.getResource(path.concat("/jcr:content"));
            ValueMap properties = resource.adaptTo(ValueMap.class);
            String title = StringUtils.EMPTY;
            String comment = StringUtils.EMPTY;
            String userId = StringUtils.EMPTY;
            if (properties.containsKey(JCRTITLE)) {
                title = properties.get(JCRTITLE, String.class) + ".html";
                LOGGER.info("jcrtitle+++++++++++++++++++++++++++++++++++++++++++" + title);
            }
            if (properties.containsKey(CQNAME)) {
                title = properties.get(CQNAME, String.class);
                LOGGER.info("cq:name+++++++++++++++++++++++++++++++++++++++++++" + title);
            }

            // Workflow wf = item.getWorkflow();
            List<HistoryItem> wfHistory = workflowSession.getHistory(item.getWorkflow());

            for (HistoryItem itemhistory : wfHistory) {
                LOGGER.info("history iem+++++++++++++++++++++++++++++++++++++++++++" + itemhistory.getUserId());
                LOGGER.info("comment+++++++++++++++++++++++++++++++++++++++++++" + itemhistory.getComment());
                comment = itemhistory.getComment();
                userId = itemhistory.getUserId();

            }

            LOGGER.info("path+++++++++++++++++++++++++++++++++++++++++++      " + path);

            String owner = item.getWorkflow().getInitiator();
            LOGGER.info("owner+++++++++++++++++++++++++++++++++++++++++++      " + owner);

            Date startdate = item.getTimeStarted();
            SimpleDateFormat mdyFormat = new SimpleDateFormat(REQDATE);
            String date = mdyFormat.format(startdate.getTime());

            LOGGER.info("startdate+++++++++++++++++++++++++++++++++++++++++++      " + date);

            try {
                // Declare a MessageGateway service
                MessageGateway<Email> messageGateway;

                // Set up the Email message
                HtmlEmail email = new HtmlEmail();
                // Set the mail values
                String emailToRecipients = "Raushan.jaiswal@gmail.com";
                String salutation = "<b>Dear  " + owner + ",</b><br>";
                String data = "<p><br><b>WorkArea:</b>\t" + path + "\t <b>Owner:\t" + userId + "\t <b>Intiated:</b>\t"
                        + date + "</p>\n";
                String message = "<b><p>" + owner + "</b> <b>has submitted content for your approval. "
                        + "Please review at your earliest convenience. After you review the content, click the Approve/Reject button below to Approve/Reject"
                        + ". If you feel that you are not the right person for approving the content, you can<a href=\"/notifications.html\"> reassign </a>this task. <b><br><br><a href= \"/notifications.html\">Approve/Reject</a>";

                comment = "<b>Description and Comments :<br>Description :\t Publishes content to one or more target nodes, optionally including a review / revision loop. "
                        + userId + date + "<br>Notes to Approver :\t" + comment
                        + "<br>Current Task: Please review and take action";
                String file = "<p><br><b>Attached Files:</b>\t</br><br>" + "<br>File Name :\t" + "<a href=\"/" + path
                        + ".html\">" + title + " </a>" + "       \t <b>Title:\t" + title + "</p>\n";

                email.addTo(emailToRecipients);
                // email.addCc(emailCcRecipients);
                email.setSubject("Please review and take action ");
                email.setFrom("hiraushan123@gmail.com");
                email.setMsg(salutation + message + data + comment + file);
                // 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) email);
                LOGGER.info("sendamil succesfully+++++++++++++++++++++");