Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Sitecore.Diagnostics;
using Sitecore.EDS.Core.Dispatch;
using Sitecore.EmailCampaign.Cm.Pipelines.SendEmail;
using Sitecore.EmailCampaign.Model.Message;
using Sitecore.EmailCampaign.Model.Web.Settings;
using Sitecore.ExM.Framework.Diagnostics;
using Sitecore.Modules.EmailCampaign;
using Sitecore.Modules.EmailCampaign.Core;
using Sitecore.Modules.EmailCampaign.Core.Crypto;
using Sitecore.Modules.EmailCampaign.Core.Personalization;
using Sitecore.Modules.EmailCampaign.Messages;
using System.Web;
namespace Sitecore.Exm.Custom.Pipelines
{
public class CustomFillEmail
{
private readonly IStringCipher _cipher;
private readonly ILogger _logger;
private readonly PipelineHelper _pipelineHelper;
private readonly EcmSettings _settings;
public CustomFillEmail(IStringCipher cipher, ILogger logger, PipelineHelper pipelineHelper, EcmSettings settings)
{
_cipher = cipher;
_logger = logger;
_pipelineHelper = pipelineHelper;
_settings = settings;
}
public void Process(SendMessageArgs args)
{
if (args.EcmMessage == null)
return;
this.GetMailFiller(args)?.FillEmail();
}
private MailFiller GetMailFiller(SendMessageArgs args)
{
HtmlMailBase ecmMessage1 = args.EcmMessage as HtmlMailBase;
if (ecmMessage1 != null)
// here we return custom mail filler
return (MailFiller) new CustomHtmlMailFiller(ecmMessage1, args, _logger, _cipher, _pipelineHelper, _settings);
MailMessageItem ecmMessage2 = args.EcmMessage as MailMessageItem;
if (ecmMessage2 != null)
return new MailMessageFiller(ecmMessage2, args, _cipher, _pipelineHelper, _settings);
return null;
}
}
public class CustomHtmlMailFiller : HtmlMailFiller
{
private readonly HtmlMailBase _htmlMailBase;
public CustomHtmlMailFiller(HtmlMailBase message, SendMessageArgs args, ILogger logger, IStringCipher cipher, PipelineHelper pipelineHelper, EcmSettings settings) :
base(message, args, logger, cipher, pipelineHelper, settings)
{
_htmlMailBase = message;
}
protected override void FillBody()
{
var utcNowStart = DateTime.UtcNow;
var body = _htmlMailBase.Body;
Email.ContentType = MessageContentType.Html;
// decode all encoded content, very unsafe
Email.HtmlBody = HttpUtility.Decode(_htmlMailBase.ReplaceTokens(body));
Util.TraceTimeDiff("SetEmailHtmlBody(ReplaceTokens(Body))", utcNowStart);
var utcNowEnd = DateTime.UtcNow;
Email.PlainTextBody = _htmlMailBase.ReplaceTokens(_htmlMailBase.AlternateText);
Util.TraceTimeDiff("SetEmailAltBody(ReplaceTokens(AlternateText))", utcNowEnd);
}
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<SendEmail>
<processor type="Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.FillEmail, Sitecore.EmailCampaign.Cm">
<patch:attribute name="type">
Sitecore.Exm.Custom.Pipelines.CustomFillEmail, Sitecore.Exm.Custom
</patch:attribute>
</processor>
</SendEmail>
</pipelines>
</sitecore>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.