首页 > 解决方案 > Google Apps Script not picking up HTML formatting in template

问题描述

I'm trying to clean up my code and decided to create a template for the emails rather than having it in the body of the code itself. A simplified version of my code looks like this:

    function emailTest() {

      var templ = HtmlService.createTemplateFromFile('testSLTMail');

        templ.name = "Fazila";
        templ.student = "Student1";
        templ.oldClass = "classA";
        templ.newClass = "classB";
        templ.sltName = "Fazila";
        templ.changeReason = "testing";

      var message = templ.evaluate().getContent();

      GmailApp.sendEmail('f.yunus@littleilford.org','Test Email', message);

And my HTML template looks like this:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body> 

    <p> Dear <?= sltName ?> </p>
    <p> <?= name ?> has requested a change to the following student's timetable: </p>
    <p><center><?= student ?>'s timetable from <strong><?= oldClass ?></strong> to <strong><?= newClass ?></strong></center></p>
    <p> The reason for this change request is: </p>
    <p><center><?= changeReason ?></center></p>
    <p> Please confirm this request is authorised below. </p>
    <p> Kind regards </p>
    <p> Fazila </p>

  </body>
</html>

However, when I get the email none of the HTML formatting is going through the email I get looks like this:

<p> Dear Fazila </p>
<p> Fazila has requested a change to the following student's timetable: </p>
<p><center>Student1's timetable from <strong>classA</strong> to <strong>classB</strong></center></p>
<p> The reason for this change request is: </p>
<p><center>testing</center></p>
<p> Please confirm this request is authorised below. </p>
<p> Kind regards </p>
<p> Fazila </p>

I'm obviously missing a step somewhere which tells the apps script to apply the HTML formatting but can't see where. Any ideas?

Thanks

标签: htmlgoogle-apps-script

解决方案


使用此电子邮件构造函数:

  MailApp.sendEmail({
    to: "f.yunus@littleilford.org",
    subject: "test",
    htmlBody: message
});

有用!


推荐阅读