Sitecore EXM – Using personalization tokens in the sender details

Sitecore EXM (Email Experience Manager) currently lets you specify you the following fields in the Sender Details part of an email campaign:

  • From name
  • From email
  • Reply to

Unfortunately, neither of the above fields allow you to use the personalization tokens, that can be set for example when sending an email campaign through a Sitecore Form. In this scenario I wanted to be able to dynamically set the from email and the reply to field, by using personalization tokens such as $form_emailaddress$.

We can extend the SendEmail pipeline to replace the tokens in the fields, however there’s one more step to be done, as the fields have server side validation on it which validate if it’s an valid email address. Luckily (when using CustomSMTP) these can be adjusted by just changing a configuration file as shown below.

using Sitecore.EDS.Core.Dispatch;
using Sitecore.EmailCampaign.Cm.Pipelines.SendEmail;
using Sitecore.Modules.EmailCampaign.Messages;

namespace Example
{
    public class ReplaceTokensInAdditionalFields
    {
        public void Process(SendMessageArgs args)
        {
            var mailMessage = args.EcmMessage as MailMessageItem;
            EmailMessage emailMessage = args.CustomData["EmailMessage"] as EmailMessage;
            emailMessage.FromAddress = mailMessage.ReplaceTokens(emailMessage.FromAddress);
            emailMessage.FromName = mailMessage.ReplaceTokens(emailMessage.FromName);
            emailMessage.ReplyTo = mailMessage.ReplaceTokens(emailMessage.ReplyTo);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:x="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:exmEnabled="http://www.sitecore.net/xmlconfig/exmEnabled/" xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:eds="http://www.sitecore.net/xmlconfig/eds/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore exmEnabled:require="yes" role:require="Standalone or ContentManagement or DedicatedDispatch">
    <pipelines>
      <SendEmail>
        <processor type="Example.ReplaceTokensInAdditionalFields, Example" resolve="true" patch:after="processor[@type='Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.FillEmail, Sitecore.EmailCampaign.Cm']" />
      </SendEmail>
    </pipelines>

    <!-- This part is only required when you're using CustomSMTP -->
    <exm>
      <eds>
        <senderProvider defaultProvider="default" eds:require="CustomSmtp">
          <providers>
            <clear />
            <add name="default" type="Sitecore.EDS.Providers.CustomSmtp.Senders.SenderProvider, Sitecore.EDS.Providers.CustomSmtp">
              <!-- Change the default regex so that it recognizes $yourtoken$ as valid email address -->
              <param desc="regexValue" set:ref="" set:value="^[\p{L}0-9\+\-_\!\$\%\&amp;\*\?]+(?:\.[\p{L}0-9\+\-_\'\!\$\%\&amp;\*\?]+)*@(([\p{L}0-9\-]+(?:[\p{L}0-9\-]*\.[\p{L}0-9]+)+)|(\[\d{1,3}(\.\d{1,3}){3}\]))|\$.+\$$" />
            </add>
          </providers>
        </senderProvider>
      </eds>
    </exm>
    <fromReplyToEmailRegexValidator type="Sitecore.Modules.EmailCampaign.Validators.RegexValidator, Sitecore.EmailCampaign" singleInstance="true">
      <!-- Change the default regex so that it recognizes $yourtoken$ as valid email address -->
      <param desc="regexValue">^[a-zA-Z0-9\+\-_\!\$\%\&amp;\*\?]+(?:\.[a-zA-Z0-9\+\-_\'\!\$\%\&amp;\*\?]+)*@(([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9]+)+)|(\[\d{1,3}(\.\d{1,3}){3}\]))|\$.+\$$</param>
    </fromReplyToEmailRegexValidator>
  </sitecore>
</configuration>

EXM Delivery Cloud / SparkPost

When you’re not using the CustomSMTP provider but the SparkPost provider (EXM Delivery Cloud), then there’s a little more work to bypass the validations. We have to extend the SenderProvider from the SparkPost provider so that it will not try to validate our token.

using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Sitecore.EDS.Core.Senders;
using Sitecore.EDS.Providers.SparkPost.Client;

namespace Example
{
    public class CustomSparkPostSenderProvider : Sitecore.EDS.Providers.SparkPost.Senders.SenderProvider
    {
        public CustomSparkPostSenderProvider(ISparkPostApiClient apiClient) : base(apiClient)
        {
        }

        public override Task<SenderValidationStatus> ValidateSenderAsync(string senderEmail)
        {
            if (!string.IsNullOrEmpty(senderEmail) && Regex.IsMatch(senderEmail, "^\\$.+\\$$"))
            {
                return Task.FromResult(SenderValidationStatus.Valid);
            }

            return base.ValidateSenderAsync(senderEmail);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:x="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:eds="http://www.sitecore.net/xmlconfig/eds/" xmlns:exmEnabled="http://www.sitecore.net/xmlconfig/exmEnabled/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> 
    <sitecore exmEnabled:require="yes" role:require="Standalone or ContentManagement or DedicatedDispatch" eds:require="EmailCloud" >
        <exm>
            <eds>
                <senderProvider defaultProvider="default">
                    <providers>
                        <clear />
                        <add name="default" set:type="Example.CustomSparkPostSenderProvider, Example">
                            <param ref="exm/eds/apiClient" />
                        </add>
                    </providers>
                </senderProvider>
            </eds>
        </exm>
    </sitecore>
</configuration>

Leave a Reply

Your email address will not be published. Required fields are marked *