Search code examples
google-apps-scriptamp-htmlamp-email

Is it possible to send dynamic emails from Apps Script?


'm building an internal request-approval system for my company, and the best scenario I'd like to develop is using the amp dynamic emails in Gmail (our company relies on G Suite services).

I made some few tests, and while sending through https://amp.gmail.dev/playground/ is working fine, when I try to send from GAS the amp content is not showing (Developer settings already enabled, my own address is white-listed). Knowing that GAS has some various limitations, I'd like to know if it's even possible to send automated dynamic emails.

function doGet(e) {          
  var body = HtmlService.createTemplateFromFile('body').evaluate().getContent()

  GmailApp.sendEmail(EMAIL_ADDRESS, new Date(), body, { htmlBody : body})          
}

the html body

<!DOCTYPE HTML>
    <html ⚡4email>
    <head>
      <meta charset="utf-8">
      <script async src="https://cdn.ampproject.org/v0.js"></script>
      <style amp4email-boilerplate>body{visibility:hidden}</style>
      <style amp-custom>
        h1 {
          margin: 1rem;
        }
      </style>
    </head>
    <body>
      <body>
  <amp-img src="https://placekitten.com/800/400"
           alt="Welcome"
           width="800"
           height="400">
  </amp-img>
</body>
    </body>
</html>

Solution

  • AMP for Email requires the AMP to be a separate part in a multipart/alternative MIME tree with text/x-amp-html as the Content-Type. See Structure and rendering of AMP emails for more information.

    The documentation for GmailApp.sendEmail has the following explanation:

    Sends an email message with optional arguments. The email can contain plain text or an HTML body. The size of the email (including headers, but excluding attachments) is quota limited.

    Therefore, it's not currently possible to include the required text/x-amp-html part in the email body using this API. The code you have now is putting the AMP code inside the text/html part which email clients will treat as regular HTML email, likely resulting in stripping the required markup and scripts.