如何使用 cfscript 而不是 cfmail 构建复杂的电子邮件 - 模板加上保存在变量中的内容?

How can I build a complex e-mail - template plus content saved in a variable - using cfscript instead of cfmail?

这是对我之前提出的问题的扩展。服务器是CF2016。我正在使用 savecontent:

保存 table 数据
savecontent variable = 'mailBody' {
   writeOutput('
    <table width="99%" style="border-collapse:collapse;width:99%;">
      <tr>
        <td style="background-color:##09AFFF;color:##FFFFFF;width:30%;padding-left:3px;padding-top:5px;padding-bottom:5px;font-size:12px;font-weight:700;border-bottom:1px solid ##5B5B5B;text-align:left;">Name</td>
        <td style="background-color:##09AFFF;color:##FFFFFF;width:15%;padding-top:5px;padding-bottom:5px;font-size:12px;font-weight:700;border-bottom:1px solid ##5B5B5B;text-align:center;">Class</td>
        <td style="background-color:##09AFFF;color:##FFFFFF;width:30%;padding-top:5px;padding-bottom:5px;font-size:12px;text-align:left;font-weight:700;border-bottom:1px solid ##5B5B5B;">City,State,ZIP</td>
        <td style="background-color:##09AFFF;color:##FFFFFF;width:15%;padding-right:5px;padding-top:5px;padding-bottom:5px;font-size:12px;text-align:left;font-weight:700;border-bottom:1px solid ##5B5B5B;">Amount</td>
      </tr>
  ');

   for ( qryPeople in queryPeople ){
       writeOutput('
      <tr>
        <td style="font-size:12px;padding-left:3px;padding-top:3px;padding-bottom:4px;background-color:#thisBgColor#;border-bottom:1px solid ##5B5B5B;">#qryPeople.p_first# #qryPeople.p_last#</td>
        <td style="font-size:12px;padding-left:3px;padding-top:3px;padding-bottom:4px;background-color:#thisBgColor#;border-bottom:1px solid ##5B5B5B;text-align:center;">#YEAR(qryPeople.p_graduation)#</td>
        <td style="font-size:12px;padding-left:3px;padding-top:3px;padding-bottom:4px;background-color:#thisBgColor#;border-bottom:1px solid ##5B5B5B;">#qryPeople.p_city# #qryPeople.p_state#</td>
        <td style="font-size:12px;padding-top:3px;padding-bottom:4px;padding-right:5px;background-color:#thisBgColor#;border-bottom:1px solid ##5B5B5B;">#NumberFormat(qryValue.p_value,'99,999')#</td>
      </tr>
    ');
   };
   writeOutput('
      <tr>
        <td colspan="5" style="font-size:11px;padding-left:5px;padding-top:5px;padding-right:5px;padding-bottom:7px;background-color:##09AFFF;color:##FFFFFF;font-style:italic;border-bottom:1px solid ##5B5B5B;">footer text</td> 
      </tr>  
    </table>
  '); 
};//end savecontent

在这里工作正常 - 我可以输出变量 mailBody 并且我看到 HTML 电子邮件的样式 table suitable。

我们有使用集中存储的 (.htm) 文件的库存电子邮件模板。我正在尝试将此内容注入这些要发送的模板之一。

    mailerService = new mail();
    mailTemplate = fileRead(application.paths.physicalroot & '\email\project1\templates\people.htm');
    mailerService.setTo("me@domain.com"); 
    mailerService.setFrom("support@domain.com"); 
    mailerService.setSubject("People Report"); 
    mailerService.setType("html"); 
    mailerService.send(body=mailTemplate);

在我的 .htm 模板文件中

<cfoutput>#mailBody#</cfoutput>

它正给我 - #mailBody#。在不太复杂的电子邮件中,我使用

之类的东西没有问题
<cfoutput>Welcome #qryPeople.p_first# #qryPeople.p_last#</cfoutput>

或访问在驱动电子邮件的 cfscript 模板上设置的其他变量。但我不明白为什么我的 savecontent 变量没有按预期工作。

解决方案 - 之前尝试保存内容包含无效,但这可能适用于 ACF 2010。这适用于 ACF2016。

mailerService = new mail();
savecontent variable="mailTemplate" {
  include variables.templatePath & '\email\project1\templates\people.htm';
};
mailerService.setTo("me@domain.com"); 
包含

People.htm 并且在电子邮件中呈现其他保存内容(邮件正文)。现在使用较新的 cfmail() 脚本解决这个问题...

如果您只有一个 "block" 需要评估,我将使用字符串函数替换它:

mailTemplate = fileRead(application.paths.physicalroot & '\email\project1\templates\people.htm');
mailTemplate = replaceNoCase(mailTemplate, "##mailBody##", mailBody, "one");
// continue with mailerService.* methods

另一种选择是将 include 与 saveContent 一起使用:

这可能需要您将模板从 *.htm 文件重命名为 *.cfm 文件。

// create mailBody first using your current saveContent
savecontent variable="finalBody" {
    include "#application.paths.physicalroot#\email\project1\templates\people.cfm";
}

变量 finalBody 现在应该包含 mailBody 变量的内容。

如果您可以在模板中使用 CF 标记,您应该能够通过以下方式获得您想要的结果:

<cfsavecontent variable="mailBody">
<cfinclude template="#application.paths.physicalroot#\email\project1\templates\people.htm"> 
</cfsavecontent>