Customization in Personalized Email Campaign

If we are using the Email Experience Manager for daily news camping and we have list of recipient  they should receive this email daily if the is only new news added
in this case we can simply overwrite the sent pipline to make sure that we have news.
but what if the list of news personalized to the recipient . in this case
 recipient  1 maybe have news while recipient 2 didn't   to solve this we can do

Added hidden field  to the generated email a

Set the value of SendEmail in page load

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EmailNewsListing.ascx.cs" Inherits="Layouts.EmailCampaign.TMF.NewsAndInsights.EmailNewsListing" %>
<input type="hidden" id="hdnSendEmail" runat="server" value="SendEmail:null" />
//your listing


if (//has items)
{
hdnSendEmail.Value = "SendEmail:true";
}
else
{
hdnSendEmail.Value = "SendEmail:false";
}

and in the sent pipeline we can check if the flag is in the SendEmail = true we send the message other wise we know that this message is empty and we can ignore sending the message
using Sitecore.Diagnostics;
using Sitecore.EDS.Core.Dispatch;
using Sitecore.ExM.Framework.Diagnostics;
using Sitecore.Modules.EmailCampaign;
using Sitecore.Modules.EmailCampaign.Core.Pipelines;
using Sitecore.Modules.EmailCampaign.Exceptions;
using Sitecore.Modules.EmailCampaign.Factories;
using Sitecore.Pipelines;
//using Sitecore.Modules.EmailCampaign.Core.Dispatch;
//using Sitecore.Modules.EmailCampaign.Core.Pipelines;
//using Sitecore.Modules.EmailCampaign.Exceptions;
//using Sitecore.Modules.EmailCampaign.Factories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace EmailExperience.Pipeline
{
public class SendEmail
{
// Fields
private readonly ILogger _logger;
private readonly Random _rand;
// Methods
public SendEmail() : this(EcmFactory.GetDefaultFactory().Io.Logger)
{
}
public SendEmail(ILogger logger)
{
this._rand = new Random();
Assert.ArgumentNotNull(logger, "logger");
this._logger = logger;
}
public void Process(SendMessageArgs args)
{
Assert.ArgumentNotNull(args, "args");
bool sendemail = false;
EmailMessage message = args.CustomData["EmailMessage"] as EmailMessage;
if (message == null)
{
args.AddMessage("Missing EmailMessage from arguments.");
}
else if (message.Recipients.Count < 1)
{
args.AddMessage("Missing Recipients from EmailMessage argument.");
}
else if (!DispatchManager.IsConfigured)
{
args.AddMessage("Dispatch manager is not configured");
}
else if (message.HtmlBody.Contains("hdnSendEmail"))
{
this._logger.LogInfo("Found the hdnSendEmail");
if (message.HtmlBody.Contains("SendEmail:null"))
{
this._logger.LogInfo("Custom Send Email null");
}
else if (message.HtmlBody.Contains("SendEmail:false"))
{
this._logger.LogInfo("Custom Send Email false");
}
else if (message.HtmlBody.Contains("SendEmail:true"))
{
this._logger.LogInfo("Custom Send Email true");
sendemail = true;
}
}
else
{
this._logger.LogInfo("hdnSendEmail not Found");
sendemail = true;
}
if (sendemail)
{
Sitecore.Modules.EmailCampaign.Core.EcmGlobals.GenerationSemaphore.ReleaseOne();
this._logger.TraceInfo(string.Format("Message dispatch has started. Subject: '{0}'. Recipient: '{1}'.", message.Subject, message.Recipients[0]));
args.StartSendTime = DateTime.UtcNow;
if (GlobalSettings.MtaEmulation || args.Task.Message.Emulation)
{
Thread.Sleep(this._rand.Next(GlobalSettings.EmulationMinSendTime, GlobalSettings.EmulationMaxSendTime));
if (this._rand.Next(0, 0x186a0) <= (GlobalSettings.EmulationFailProbability * 1000.0))
{
throw new NonCriticalException("Emulation failed", new object[0]);
}
}
else
{
DispatchResult result = DispatchManager.SendAsync(message).Result;
//check
//args.Task.Summary.AddMailSizeSend(int.Parse(result.Statistics.Get(1)));
}
args.AddMessage("ok", PipelineMessageType.Information);
this._logger.TraceInfo(string.Format("Message dispatch has been completed. Subject: '{0}'. Recipient: '{1}'.", message.Subject, message.Recipients[0]));
args.SendingTime = Util.GetTimeDiff(args.StartSendTime, DateTime.UtcNow);
}
}
}
}
view raw SendEmail.cs hosted with ❤ by GitHub
then you have to update your configurations
<SendEmail>
<processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.FillEmail, Sitecore.EmailCampaign.Cm">
<param desc="cipher" ref="exmAuthenticatedCipher" />
</processor>
<!--<processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.SendEmail, Sitecore.EmailCampaign.Cm" />-->
<processor type="YourNamespace.EmailExperience.Pipeline.SendEmail, YourNamespace.EmailExperience" />
<processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.CreateTask, Sitecore.EmailCampaign.Cm">
<param ref="exm/sentMessagesTaskPool"/>
<param desc="logger" ref="exmLogger" />
</processor>
</SendEmail>


Comments

Popular posts from this blog

Sitecore SaaS Product

SIF Certificate Root expired

Sitecore 9 Update 2 Installation Guide Step by step