Friday, April 29, 2022

Send emails through messaging API via HTTP instead of the Sitecore OOTB CustomSMTP

How to send email over HTTP by leveraging Sitecore EXM features?


Config changes:

File Path : \App_Config\Sitecore\EmailExperience\Sitecore.EmailExperience.ContentManagement.config

       1. Comment out existing processor

                    <!-- <processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.SendEmail, Sitecore.EmailCampaign.Cm" resolve="true" /> -->

        2. Add your new processor(which will hold external API calling logic)

                    <processor type="Foundation.Notification.Pipelines.ExmSendEmailProcessor, Foundation.Notification" resolve="true" />


<SendEmail>

        <processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.FillEmail, Sitecore.EmailCampaign.Cm" resolve="true" />

        <!-- <processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.SendEmail, Sitecore.EmailCampaign.Cm" resolve="true" /> -->

		<processor type="Foundation.Notification.Pipelines.ExmSendEmailProcessor, Foundation.Notification" resolve="true" />

        <processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.Sleep, Sitecore.EmailCampaign.Cm">

              <!-- Number of milliseconds to put the thread to sleep for after an email has been sent. -->

              <param desc="sleep">50</param>

          </processor>

      </SendEmail>

Pipeline/Processor logic:
Call external API and pass the required details.
public class ExmSendEmailProcessor
    {
        public void Process(SendMessageArgs args)
        {
            Assert.ArgumentNotNull((object)args, nameof(args));
            MailNotification mailNotification = new MailNotification();
            if (!(args.CustomData["EmailMessage"] is EmailMessage message))
                args.AddMessage("Missing EmailMessage from arguments.");
            else if (message.Recipients.Count < 1)
                args.AddMessage("Missing Recipients from EmailMessage argument.");
            else
            {
                CommunicationModel model = new CommunicationModel();
                model.To = string.Join(",", message.Recipients);
                Logger.ABCLogger.Info($"EXM-model.To : {model.To}");
                model.NotifyContent = message.HtmlBody;
                Logger.ABCLogger.Info($"EXM-model.NotifyContent : {message.HtmlBody}");
                model.Subject = message.Subject;
                Logger.ABCLogger.Info($"EXM-model.Subject : {message.Subject}");
                model.From = message.FromAddress;
                Logger.ABCLogger.Info($"EXM-model.From : {message.FromAddress}");
                bool status = mailNotification.Notify(model);
                Logger.ABCLogger.Info($"mailNotification.Notify(model) status : {status}");
            
            //Call the external api and pass required details

            }
        }
    }

No comments:

Post a Comment

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally whi...