我似乎无法成功让 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>
最佳答案
这个修改怎么样?我认为您的问题的原因是 <?=
的<?= 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/