EXM – Send a confirmation email to the contact when he unsubscribes from all marketing preferences (opt-out)

It is of course sad to see a contact unsubscribing from all email communcation on your website, but what if you could send him a nice confirmation email that might change his mind?

The Sitecore Email Experience Manager is capable of sending confirmation mails to the contact when a few actions happen:

  • Subscription confirmation
    This happens when a contact subscribes to a contact list while requiring a confirmation of the subscription (double opt-in).
  • Subscription notification
    This happens when a contact subscribes to a contact list while no confirmation is required.
  • Unsubscribe notification
    This happens when a contact ubsubscribes from a contact list.

These emails can be configured within the Manager Root, and they are located within the Manager Root/Messages/Service Messages/Self-Service subscription. Please note that these emails will not be sent, unless you enable the Send unsubscribe confirmations checkbox on the Manager Root.

The Preference Center however, does not unsubscribe you from any lists but sets your marketing preferences. When you click the “Unsubscribe from all” button, Sitecore will add the contact to the Global Opt Out List that is configured on the Manager Root, and will remove all subscriptions to marketing preferences for that contact.

Implementation

As stated above, the Preference Center does not unsubscribe you from any list, meaning that the confirmation emails from EXM will not be sent!
Sitecore being Sitecore, there’s an easy way to extend its functionality and to add the required functionallity.

We can create a custom implementation of the SubscriptionManager class and override the UnsubscribeFromAll method, where we can add the functionallity to send an email to the contact. Please see the example below.

CustomSubscriptionManager.cs

using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.EmailCampaign.Cm;
using Sitecore.EmailCampaign.Cm.Factories;
using Sitecore.ExM.Framework.Diagnostics;
using Sitecore.Modules.EmailCampaign;
using Sitecore.Modules.EmailCampaign.Core;
using Sitecore.Modules.EmailCampaign.Core.Contacts;
using Sitecore.Modules.EmailCampaign.Factories;
using Sitecore.Modules.EmailCampaign.ListManager;
using Sitecore.Modules.EmailCampaign.Messages;
using Sitecore.Modules.EmailCampaign.Services;
using Sitecore.XConnect;

namespace Example
{
    public class CustomSubscriptionManager : SubscriptionManager
    {
        private readonly IExmCampaignService _exmCampaignService;
        private const string UnsubscribeFromAllMessageSetting = "StandardMessages.UnsubscribeFromAllNotification";

        public CustomSubscriptionManager(IContactService contactService,
            ILogger logger,
            ListManagerWrapper listManagerWrapper,
            IExmCampaignService exmCampaignService,
            PipelineHelper pipelineHelper,
            ISendingManagerFactory sendingManagerFactory,
            IManagerRootService managerRootService,
            IRecipientManagerFactory recipientManagerFactory) : base(contactService,
            logger, listManagerWrapper, exmCampaignService, pipelineHelper, sendingManagerFactory, managerRootService, recipientManagerFactory)
        {
            _exmCampaignService = exmCampaignService;
        }

        public override bool UnsubscribeFromAll(Contact contact, ManagerRoot managerRoot)
        {
            var result = base.UnsubscribeFromAll(contact, managerRoot);
            if (result)
            {
                var message = GetStandardMessage(UnsubscribeFromAllMessageSetting, managerRoot);
                SendInfoMessage(message, contact);
            }

            return result;
        }

        private MessageItem GetStandardMessage(string configSetting, ManagerRoot managerRoot)
        {
            string setting = Settings.GetSetting(configSetting, string.Empty);
            if (string.IsNullOrEmpty(setting))
            {
                return null;
            }
            setting = string.Concat(managerRoot.InnerItem.Axes.SelectSingleItem("*[@@tid='{D8AD4B81-9269-4868-949F-37D1C28687E5}']").Paths.FullPath.TrimEnd(new[] { '/' }), "/", setting.TrimStart('/'));
            Item item = Util.GetContentDb().GetItem(setting);
            if (item == null)
            {
                return null;
            }
            return this._exmCampaignService.GetMessageItem(item);
        }
    }
}

Feature.EXM.CustomSubscriptionManager.config

<?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:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore exmEnabled:require="yes" role:require="Standalone or ContentManagement or DedicatedDispatch">
    <exm>
      <subscriptionManager set:type="Example.CustomSubscriptionManager, Example" resolve="true"/>
    </exm>
    <setting name="StandardMessages.UnsubscribeFromAllNotification" set:value="Service Messages/Self-Service Subscription/Unsubscribe From All Notification" />
  </sitecore>
</configuration>

Now the hard part is done, all you have to do is to create a new email message in the Manager root.
Make sure it has the same name as is configured in the configuration file in the example above, that it is an Automated Email, and that you’ve selected the Service Message checkbox in the Delivery tab. Also don’t forget to activate the email!

Leave a Reply

Your email address will not be published.