Email displays br tags in text

Kentico User asked on March 28, 2016 16:57

Hi,

I have a biz form and when that form is submitted it triggers a CMSModule. The module finds out 5 different alias paths and gets information from HTML fields which are part of a custom page type.

This is the following code

if (!string.IsNullOrEmpty(answer6NodeAliasPath))
                {
                    node6 = tp.SelectSingleNode(CMS.SiteProvider.SiteContext.CurrentSiteName, answer6NodeAliasPath, "");
                    node6Value = node6.GetStringValue("AnswerExplanation", "");
                }

                string[,] macros = new string[,] { 
                    { "text1", WebUtility.HtmlEncode(node1Value) }, 
                    { "text2", WebUtility.HtmlEncode(node2Value) },
                    { "text3", WebUtility.HtmlEncode(node3Value) },
                    { "text4", WebUtility.HtmlEncode(node4Value) },
                    { "text5", WebUtility.HtmlEncode(node5Value) },
                    { "text6", WebUtility.HtmlEncode(node6Value) }
                };

                MacroResolver macroResolver = new MacroResolver();
                macroResolver.SetNamedSourceData(macros);

                if (emailTemplate != null)
                {
                    EmailMessage emailMessage = new EmailMessage();
                    emailMessage.EmailFormat = EmailFormatEnum.Html;
                    emailMessage.Recipients = emailFieldValue;
                    emailMessage.From = emailTemplate.TemplateFrom;
                    emailMessage.Subject = emailTemplate.TemplateSubject;
                    EmailSender.SendEmailWithTemplateText(CMS.SiteProvider.SiteContext.CurrentSiteName, emailMessage, emailTemplate, macroResolver, true);
                }

I am using an HTML email template which is very basic right now

<p>{%text1%}</p>
<p>{%text2%}</p>
<p>{%text3%}</p>
<p>{%text4%}</p>
<p>{%text5%}</p>
<p>{%text6%}</p>

The value of text1 is HTML data with HTML tags. The tags are not getting rendered in the email. The email shows up as My organization has a strong service vision and values<br /> 1. You scored yourself: Agree<br />

Any ideas?

Thank you

Correct Answer

Jan Hermann answered on March 30, 2016 12:32

Hello,

Or you can add the encode parameter to a macro:

<p>{%text1|(encode)false%}</p>
<p>{%text2|(encode)false%}</p>
<p>{%text3|(encode)false%}</p>
<p>{%text4|(encode)false%}</p>
<p>{%text5|(encode)false%}</p>
<p>{%text6|(encode)false%}</p>

Best regards,
Jan Hermann

1 votesVote for this answer Unmark Correct answer

Recent Answers


Anton Grekhovodov answered on March 28, 2016 19:25

Hi,

You call WebUtility.HtmlEncode method which converts HTML string to HTML-encoded string, so you have incorrect data.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.