google-apps-script - Apps 脚本电子邮件 : HTML String not evaluating in Template Body

标签 google-apps-script

我似乎无法成功让 html 字符串显示在模板内。所有非 html 字符串值似乎都能正确评估并显示在电子邮件中。我浏览了文档/Stack Overflow,似乎找不到任何具体的内容。

应用程序脚本代码:

function sendReceiptEmail(order_id, data){
  const exceptions = ["name", "address", "email", "total", "building_code", "status"];

  var items = '';
  for (item in data) {
    if (exceptions.indexOf(item) == -1){
      items += "<tr><td>" + item + "</td><td>" + data[item] + "</td></tr>"
    }
  }
  var htmlBody = HtmlService.createTemplateFromFile('customer-email');

  htmlBody.name = data.name;
  htmlBody.order_id = order_id;
  htmlBody.address = data.address;
  htmlBody.email = data.email;
  htmlBody.total = data.total;
  htmlBody.building_code = data.building_code;
  htmlBody.timestamp = getFormattedDate(new Date());
  htmlBody.items = items;

  var message = htmlBody.evaluate().getContent();
  var subject = "Order #" + order_id;

  MailApp.sendEmail({
    to: data.email,
    subject: subject,
    htmlBody: message
  });
};

HTML 模板

<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h2>Thanks for your order, <?= name ?></h2>
    <p>Please see your order details below.</p>

    <p><strong>Name: </strong><?= name ?></p>
    <p><strong>Email: </strong><?= email ?></p>
    <p><strong>Address: </strong><?= address ?></p>
    <p><strong>Building Code: </strong><?= building_code ?></p>
    <p><strong>Date: </strong><?= timestamp ?></p>
    <br/>
    <p><strong>Subtotal: </strong>$ <?= total ?></p>
    <p><strong>Delivery Fee: </strong>$ 5.00</p>
    <p><strong>Total: </strong>$ 5.00</p>

    <h3><strong>Order Summary</strong></h3>
    <table>
      <tr>
        <th>Item</th>
        <th>Quantity</th>
      </tr>
      <?= items ?>
    </table>

  </body>
</html>

结果: enter image description here

最佳答案

这个修改怎么样?我认为您的问题的原因是 <?=<?= items ?>The official document说如下。

Printing scriptlets, which use the syntax , output the results of their code into the page using contextual escaping.

此时请修改为<?!=The official document说如下。

Force-printing scriptlets, which use the syntax <?!= ... ?>, are like printing scriptlets except that they avoid contextual escaping.

来自:

<?= items ?>

致:

<?!= items ?>

引用:

关于google-apps-script - Apps 脚本电子邮件 : HTML String not evaluating in Template Body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61153813/

相关文章:

google-apps-script - DriveApp 访问被拒绝

google-apps-script - Google 脚本侧边栏图像

javascript - 从包含特定字符的字符串中获取单词。 (例如#或@,例如后期规划师)

google-apps-script - 使用变量时范围的坐标或尺寸无效

javascript - 3个依赖于Javascript的下拉列表

google-maps - 天然气的安全性

Javascript:如何检查 fct 是否正在运行,如果没有运行,则使其运行?

google-apps-script - 谷歌表格=间接()在单元格范围上

javascript - Google 脚本 - Google 表格 - 需要修改脚本以跳过任何具有匹配变量的文本行

javascript - Google Apps 脚本库和脚本分发