我似乎无法成功地让html字符串显示在模板中。所有非html字符串的值似乎都正确计算并显示在电子邮件中。我已经查看了文档/堆栈溢出,似乎找不到关于这个的任何具体内容。
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>
结果: