1

We have a requirement to programmatically send 'content sharing' emails using EXM. These emails are to include content from several selected content pages (title, intro, link).

I'm an EXM novice and just trying to work out the best approach for this.

I've looked into using email tokens but these are for plain text fields and wouldn't be suitable for injecting blocks of HTML.

I've also looked into creating a new email template for each email and adding the custom HTML to the body of the new template, but I'm wondering if that's overkill or if there is a better way?

Ideally I would like to create an email template that would allow me to pass some page IDs to it and then dynamically pull content from those pages and add this to a repeat block of HTML but I haven't seen any articles that suggest something like this would be possible.

This is to be added to a Sitecore 9.2 instance.

  • Wouldn't a "smart" component do this for you? The component (which can be dropped in the EXM template) will read through the content tree and render the info required. You could even allow the component to accept some rules (e.g. template ids) – josedbaez Dec 6 '19 at 13:30
  • By smart component, do you mean a controller rendering? And if controller renderings can be used in EXM templates could I access the custom tokens in the controller method? That would definitely help me achieve what I need. – R. Allinson Dec 6 '19 at 13:38
  • Yes, you can use renderings in EXM, just like you do with standard "pages". – josedbaez Dec 6 '19 at 13:44
1

You can use Controller renderings as shared in the comments. You can also personalize those renderings so that you can show certain content to a user that fits a profile or has completed a goal, like a member vs a non-member.

| improve this answer | |
0

After lots of research the only solution I found was this custom pipeline https://gist.github.com/Mickey-P/6adb8081c5bec69b366c50ba337a5202. It HTML decodes the message before sending, not very safe but I added some logic to ensure this would only be used for certain email templates.

I had to update this line:

Email.HtmlBody = HttpUtility.Decode(_htmlMailBase.ReplaceTokens(body));

To this:

Email.HtmlBody = HttpUtility.HtmlDecode(_htmlMailBase.ReplaceTokens(body));

I also had to create a reference to the Sitecore.Email.Campaign.Cm.dll as the 'NoReferences' nuget package I installed had different dependencies to the dll in my web root (I checked the version and the revision but the nuget package wasn't the same for some reason).

After implementing this I could then use the following method to send emails with HTML injected into them:

public void SendCustomEmail(Sitecore.XConnect.Contact contact)
{
    var automatedMessage = new AutomatedMessage
    {
        ContactIdentifier = contact.Identifiers.First(),
        MessageId = new System.Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"),
        TargetLanguage = Sitecore.Context.Language.Name,
        CustomTokens = new Dictionary<string, object>
        {
            { "token1", "<strong>Html element 1</strong>" },
            { "token2", "<div><p>Html element 2</p></div>" }
        }
    };

    IClientApiService clientApiService = (IClientApiService)ServiceLocator.ServiceProvider.GetService(typeof(IClientApiService));
    clientApiService.SendAutomatedMessage(automatedMessage);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.