Shadi
Shadi

Reputation: 89

HTML Email message coming out as "HtmlOutput"

I have the following html file:

  <html>
  <head>
    <script type="application/ld+json">
    {
      "@context":       "http://schema.org",
      "@type":          "EmailMessage",
      "description":    "Click to view request details",
      "potentialAction": {
        "@type": "ViewAction",
        "target":   "<?= detailsurl ?>",
        "name": "View request details"
      }
    }
    </script>
  </head>
  <body>
    <p>
      <?= emailbody ?>
    </p>
  </body>
</html>

and the following function in my script:

  function SendRichEmail(ToEmail,EmailSubject,detailsurl,emailbody) {
  var templatefile = HtmlService.createTemplateFromFile('mail_template.html');
  Logger.log(templatefile);
  templatefile.detailsurl = detailsurl;
  templatefile.emailbody = emailbody;
  Logger.log(templatefile);
  htmlBody = templatefile.evaluate(); 
  Logger.log(htmlBody);

  MailApp.sendEmail({
    to: ToEmail,
    subject: EmailSubject,
    htmlBody: htmlBody,
    name: "Support", 
    noReply: true
  });
}

But when its triggered, the email I get is simply the string "HTMLOutput", and when i check the log I get this:

[16-05-10 11:46:02:727 EAT] {}

[16-05-10 11:46:02:728 EAT] {detailsurl=http://www.correcturl.com/, emailbody=yada yada yada

[16-05-10 11:46:02:734 EAT] HtmlOutput

I'm stumped. I'm assuming the issue is with reading or evaluating the template, but I can't pinpoint it. Appreciate the help.

Upvotes: 0

Views: 127

Answers (1)

Shadi
Shadi

Reputation: 89

As I suspected, it was a stupid oversight. I was passing an object instead of a string. Solved by changing the following line:

htmlBody = templatefile.evaluate(); 

to this:

htmlBody = templatefile.evaluate().getContent(); 

Now it works perfectly.

Upvotes: 1

Related Questions