Developer forum

Forum » Development » Use multiple languages in Emailmarketing

Use multiple languages in Emailmarketing

Jesse Bakker
Reply

Hi,

We have a website with multiple languagelayers. Where we want to send out emailmarketing from.
On the recipient users we store their preferred language. 

When sending out the emails it only sends out the original selected language of the email page.
But we want to send the translated version of the page to the recipient based on there preferred language.

We've created an notification subscriber for "OnBeforeMerging" to change the page before sending to user. 
We can get the translated body for the message, but when we update the HtmlBody of the message it still sends out the original email body.

Are we missing something? Or is there a different way to change the body of email send? 

[Subscribe(Notifications.OnBeforeMerging)]
public class OnBeforeMergingSubscriber : NotificationSubscriber
{
    public override void OnNotify(string notification, NotificationArgs args)
    {
        if (!(args is Notifications.OnBeforeMergingNotificationArgs mergingArgs))
        {
            return;
        }

        var email = Dynamicweb.EmailMarketing.Email.GetEmailByMessageId(mergingArgs.Message.Id);
        var userLanguage = mergingArgs.Recipient.TagValueCollection["Email:User.AccessUser_LanguageCode"]?.ToString();

        if (email.Page.Area.Culture != userLanguage)
        {
            var preferedLangaugePage = email.Page.Languages.FirstOrDefault(x => x.Area.Culture == userLanguage));

            if (preferedLangaugePage != null)
            {
                var recipientContext = email.RecipientProvider.GetRecipientContentContext(mergingArgs.Recipient);
                var translatedBody = Renderer.BuildPageHtml(preferedLangaugePage.ID, email.Template, recipientContext);

                mergingArgs.Message.HtmlBody = translatedBody;
                mergingArgs.Message.Save();
            }
        }
    }
}

Replies

 
Nicolai Pedersen
Reply

This is the before merge - you set the htmlbody to your result - but DW does the same, but after this notification, because this is the before... So you need to find a later notification.

That said, why not use 2 or more smart searches - one for each language, and then have 2 or more emails using each their language version? 

BR Nicolai

 
Jesse Bakker
Reply

Thank you for your answer Nicolai. 

Does an notification exists later in the cycle that we can use? Or is there a way to let DW skip setting the html body?

We also suggested the client to use multiple emails for this. But they really want it in a single email if possible. 

 
Nicolai Pedersen
Reply

Hi Jesse

Does not seem to be a later notification. Try using the PreprocessedHtmlBody property of the message object in the passed arguments.

That said, your solution does feel a little 'intrusive' and could cause other problems. Setting up one email for each language would be fast and less error prone.

BR Nicolai

 

You must be logged in to post in the forum