我需要根据条件生成电子邮件。这是电子邮件模板:
<html>
<head>
</head>
<body style="font-size: 12px; font-family: arial">
<p>
This is an automatic notification sent by Kentico. The following document is waiting for your approval. Please sign in to the Kentico administration interface and approve it.
</p><br />
{%if (2<3) { %}
<p>
<b><i>This is Sent By : {%SentBy%}</i></b><br /><br />
<a href={%siteurl%}>SiteURL</a><br />
<br />
</p>
{% }else{%}
<p>This is else Method</p>
{%}#%}
</body>
</html>
这里有代码:
EmailMessage msg = new CMS.EmailEngine.EmailMessage();
EmailTemplateInfo etInfo = EmailTemplateProvider.GetEmailTemplate("Email", SiteContext.CurrentSiteID);
if (etInfo != null)
{
MacroResolver mcr = MacroResolver.GetInstance();
mcr.SetNamedSourceData("siteurl", "http://google.com/");
mcr.SetNamedSourceData("SentBy", "admin");
msg.EmailFormat = EmailFormatEnum.Both;
msg.From = etInfo.TemplateFrom;
msg.Recipients = "xyz@google.com";
msg.Subject = etInfo.TemplateSubject;
msg.Body = etInfo.TemplateText;
msg.PlainTextBody = etInfo.TemplatePlainText;
//Send Email..
EmailSender.SendEmailWithTemplateText(SiteContext.CurrentSiteName, msg, etInfo, mcr, true);
}
当我发送电子邮件时,它会呈现如下:
This is an automatic notification sent by Kentico. The following document is waiting for your approval. Please sign in to the Kentico administration interface and approve it.
<p> <b><i>This is Sent By : admin</i></b><br /><br /> <a href=http://google.com/>SiteURL</a><br /> <br /> </p>
&#34;下面的所有HTML标记;如果&#34;条件没有呈现。
答案 0 :(得分:2)
要获取已解析宏的文本,您还需要调用ResolveMacros
方法。
在你的情况下,它可能是这样的:
EmailMessage msg = new CMS.EmailEngine.EmailMessage();
EmailTemplateInfo etInfo = EmailTemplateProvider.GetEmailTemplate("Email", SiteContext.CurrentSiteID);
if (etInfo != null)
{
MacroResolver mcr = MacroResolver.GetInstance();
mcr.SetNamedSourceData("siteurl", "http://google.com/");
mcr.SetNamedSourceData("SentBy", "admin");
msg.EmailFormat = EmailFormatEnum.Both;
msg.From = etInfo.TemplateFrom;
msg.Recipients = "xyz@google.com";
msg.Subject = etInfo.TemplateSubject;
msg.Body = mcr.ResolveMacros(etInfo.TemplateText);
msg.PlainTextBody = mcr.ResolveMacros(etInfo.TemplatePlainText);
//Send Email..
EmailSender.SendEmailWithTemplateText(SiteContext.CurrentSiteName, msg, etInfo, mcr, true);
}
请注意我正在使用的mcr.ResolveMacros(etInfo.TemplateText)
代码。
答案 1 :(得分:0)
通常我会将编码参数添加到宏中:
{% some_macro_with_html_output|(encode)false %}
我不确定它是否适用于您的记录类型。尝试在关闭else语句的标记之前添加它。