2016-12-05 1 views
1

条件に基づいて電子メールを生成する必要があります。ここでは、電子メールのテンプレートは次のとおりです。kentico8の電子メールテンプレートでif-elseステートメントでHTMLタグがレンダリングされない

<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 = "[email protected]"; 
      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> 

すべてのHTML if条件の下にあるタグは表示されません。

答えて

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 = "[email protected]"; 
      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)コードを注意してください。

+0

ありがとうございます。正常に動作します。 – sadasiva

+0

それを聞いてうれしい。その場合、あなたは答えを受け入れることができます:) – Enn

0

通常、私はマクロにエンコードパラメータを追加したい:

{% some_macro_with_html_output|(encode)false %} 

私はわからないんだけど、それは、レコードのあなたのタイプで動作するかどうか。 else文のタグを閉じる前に追加してください。

関連する問題