Send your first interactive AMP Email with Twilio SendGrid
Tags: email, amp, javascript, html
Published: 2020-11-11 06:23:03

---
title: Send your first interactive AMP Email with Twilio SendGrid
published: true
description: 
tags: email, amp, javascript, html
cover_image: https://twilio-cms-prod.s3.amazonaws.com/images/1-3B4YRfy1_1izzXfgJk3uk-TggOuRPtWn2livv_6M_bP7.width-808.png
canonical_url: https://www.twilio.com/blog/send-interactive-amp-email-twilio-sendgrid
---

[AMP for email](https://amp.dev/about/email/) is a way to build emails that allow users to interact dynamically with the content of the message. AMP emails can load up-to-date data, handle form submissions inline, provide interactive components like carousels and accordions, and even use modern CSS.

AMP for email is a combination of a [whitelisting process](https://amp.dev/documentation/guides-and-tutorials/start/email_sender_distribution/?format=email) and a subset of the open source [AMP HTML web component library](https://amp.dev/documentation/components/?format=email) that together allows you to build and send dynamic emails. You may have seen examples of this in your inbox before — my favourite is this experience when commenting on a Google Doc.

![An animation, viewing a Google Docs comment email in Gmail. In the animation I reply to a comment then mark the thread as resolved all within the email.](https://twilio-cms-prod.s3.amazonaws.com/original_images/h3nl8ST0yWSPhB9Vi91rVe32IGvj9a5lSqAjhejN5OQ8yTQX3JVYjTj1cPo4V0yedeOAVMJSyDxjXS)

You too can create email experiences like this. In this article we're going to send a basic AMP Email using [Twilio SendGrid](https://www.twilio.com/sendgrid/email-api).

## What you'll need

To build your first AMP Email and send it with SendGrid you will need:

*   [A SendGrid account](https://signup.sendgrid.com/) with a [dedicated IP address](https://sendgrid.com/docs/ui/account-and-settings/dedicated-ip-addresses/) (this requires at least a Pro Email API or Advanced Marketing Campaigns plan, please [see the pricing page for more details](https://sendgrid.com/pricing/))
*   A [Gmail account](https://www.google.com/intl/en-GB/gmail/about/) with which you can test receiving AMP emails
*   A domain from which you can send email
*   [Node.js](https://nodejs.org/) (for sending the email)

Dynamic emails are subject to [additional security requirements and restrictions](https://developers.google.com/gmail/ampemail/security-requirements). You will need to set up the following security measures for your domain:

*   [Sender Authentication for your domain](https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/)
*   [DMARC for your domain](https://sendgrid.com/docs/ui/sending-email/how-to-implement-dmarc/)

You will need to have both Sender Authentication (SPF and DKIM) as well as DMARC set up in order to successfully send AMP emails. If these are not setup properly then the inbox will not render the dynamic part of the email at all.

> When building AMP emails there are a number of places to test: the official [AMP playground](https://playground.amp.dev/), the [Gmail AMP Playground](https://amp.gmail.dev/playground/), and the [Mail.ru AMP playground](https://postmaster.mail.ru/amp/playground.html?lang=en#hello-world). The playgrounds can preview what an AMP email will look like and how it will behave, the Gmail and Mail.ru playgrounds can also send AMP email to your test email address. While we are building with SendGrid in this post, you could test your AMP emails in any of these playgrounds.
>
> If you do not have a SendGrid account with a dedicated IP address you will not be able to send AMP emails from your SendGrid account. You can still follow this post and send test AMP emails from the [Gmail AMP Playground](https://amp.gmail.dev/playground/) though.

To receive AMP emails in your Gmail account follow these instructions to [whitelist an email address from which you can receive AMP email](https://developers.google.com/gmail/ampemail/testing-dynamic-email).

Once you have that all setup, let's take a look at AMP email.

## How does AMP Email work?

You may already know that emails can have multiple parts; plain text, with a MIME type of `text/plain`, and HTML, with a MIME type of `text/html`. Email clients choose which part to display based on their capabilities (yes, there are people out there using [text only email clients](https://en.wikipedia.org/wiki/Text-based_email_client)) or user preferences.

AMP email builds upon this by adding a third part: AMP HTML with the MIME type `text/x-amp-html`. This part is supported and will be rendered in Gmail (on the web and in the Gmail native mobile applications), Mail.ru and Yahoo! Mail. If a user is using one of the supported mail clients, they can receive and interact with your AMP email, otherwise they will just get the HTML and text parts.

Let's see this in action by writing and sending an AMP email.

## Sending AMP emails

We'll work with an example project that is already setup to send an email with text and HTML parts. The [project is available on GitHub](https://github.com/philnash/exploring-amp-email/) but to work with it for this post, follow these instructions:

Clone the project and checkout the `first-amp-email` tag:

```bash
git clone https://github.com/philnash/exploring-amp-email.git -b first-amp-email
```

Change into the `first-amp-email` directory and install the dependencies:

```bash
cd first-amp-email
npm install
```

Copy the `.env.example` file to `.env` and fill it in with:

*   A [SendGrid API Key](https://app.sendgrid.com/settings/api_keys) that can send email
*   A from email address, this should be from the domain you have configured in SendGrid and that you whitelisted in the testing Gmail account
*   A to email address, the Gmail account you set up to receive AMP emails earlier

The files we are interested in are in the `first-amp-email` directory. There is a script, `send-email.js` that loads two files from the `emails` directory, a text email and an HTML email, and [sends them as one email using the SendGrid Node.js library](https://www.twilio.com/blog/how-to-send-emails-in-javascript-with-sendgrid).

Run the script with:

```bash
npm run send
```

This will send the email to the email address you set as the `TO_EMAIL` in your `.env` file. Open your Gmail inbox and you will find the email.

### Writing an AMP email

Let's take the HTML email and turn it into an AMP HTML email. Copy the existing file from `emails/email.html` to `emails/email.amp.html` and open it in your text editor.

There are a few things we need to do to turn this HTML to AMP HTML and the first is in the `<html>` tag itself. To signify this is an AMP email, we add the `⚡4email` (or `amp4email`) attribute to the `<html>` element.

```html
<!DOCTYPE html>
<html ⚡4email>
  <head>
```

Next, we need to add the AMP HTML JavaScript which loads the basic AMP HTML components. Add this to the `<head>`:

```html
  <head>
    <meta charset="utf-8" />
    <script async src="https://cdn.ampproject.org/v0.js"></script>
  </head>
```

We need one further addition to the `<head>` to make this a valid AMP email. Due to the way AMP works, it expects the contents to be hidden until it is all loaded. For AMP email we make the body of the email hidden to start with and then the framework takes over to show it when it is ready.

```html
  <head>
    <meta charset="utf-8" />
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <style amp4email-boilerplate>
      body {
        visibility: hidden;
      }
    </style>
  </head>
```

The three changes to the email:

*   the `⚡4email` attribute
*   the AMP script
*   the style boilerplate

are all we need to turn our HTML email into an AMP email.

Let's update the script to send this part along with the text and HTML. Open `send-email.js` and inside the `send` function, load the content of `emails/email.amp.html`:

```javascript
const send = async (from, to) => {
  const text = await readFile(resolve(join("emails", `email.txt`)), {
        encoding: "utf-8",
  });
  const html = await readFile(resolve(join("emails", `email.html`)), {
        encoding: "utf-8",
  });
  const amp = await readFile(resolve(join("emails", "email.amp.html")), {
    encoding: "utf-8",
  });
```

In the call to `sendgrid.send` function, add the new content type to the `content` array as shown below:

```javascript
await sendgrid.send({
  to: to,
  from: from,
  subject: "Sending Emails",
  content: [
    {
      type: "text/plain",
      value: text,
    },
    {
      type: "text/html",
      value: html,
    },
    {
      type: "text/x-amp-html",
      value: amp,
    },
  ],
  // ...
});
```

Run the command to send the email again.

```bash
npm run send
```

When you check your inbox this time, you should find your AMP email. Since this AMP email isn't doing anything special, you can check for the dynamic email icon.

![If the email has a lightning icon next to the date it was received, then it is an AMP email.](https://twilio-cms-prod.s3.amazonaws.com/images/N1r80QtgWX6XnOTZDD3WFTA1fluKxJJtHoS_3sqqVNpr8D.width-500.png)

### Debugging AMP emails

If you see an error message at the top of the email you can click through to get an idea of what has gone wrong.

Since SPF, DKIM and DMARC rely on DNS, you may have set them up correctly but need to wait for them to propagate.

If you get a warning that your email is invalid AMP, download the whole email and import the `.eml` file to the [AMP Playground](https://playground.amp.dev/?runtime=amp4email). The playground has the AMP validator built in and will show you any issues as well as previewing the output.

### Using AMP components

While that first email was an AMP email, we didn't use any AMP components. Let's make an interactive image carousel to show relevant images alongside our email content. To do this, we will use the `<amp-img>` and `<amp-carousel>` components.

The `<amp-img>` comes as part of the main AMP HTML script, but to add the [`<amp-carousel>` component](https://amp.dev/documentation/components/amp-carousel/?format=email) we need to add the component's script to the `<head>`.

```html
<head>
  <meta charset="utf-8" />
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script
    async
    custom-element="amp-carousel"
    src="https://cdn.ampproject.org/v0/amp-carousel-0.2.js"
  ></script>
  <style amp4email-boilerplate>
```

Now we can add the components to the document. Start with the `<amp-carousel>` then place two `<amp-img>` components inside. Add this to the bottom of the `<body>` of the email:

```html
  </p>

  <amp-carousel
    height="427"
    width="640"
    layout="responsive"
    type="slides"
    controls
  >
    <amp-img
      height="427"
      width="640"
      src="https://raw.githubusercontent.com/philnash/exploring-amp-email/main/first-amp-email/images/red-door.jpg"
      alt=""
    ></amp-img>
    <amp-img
      height="427"
      width="640"
      src="https://raw.githubusercontent.com/philnash/exploring-amp-email/main/first-amp-email/images/spam.jpg"
    ></amp-img>
  </amp-carousel>
</body>
```

You'll notice that we had to set the height and width of the carousel and images. When used on websites, AMP is concerned with performance. This includes ensuring pages don't go through multiple layout processes. One of the ways this is achieved is by being strict about setting sizes on images. Above we also use the `layout="responsive"` attribute on the carousel. This allows for the carousel to stretch and shrink in response to the size of the viewport keeping the aspect ratio set by the static height and width. The AMP documentation has [a good example of the different layouts available](https://amp.dev/documentation/guides-and-tutorials/learn/amp-html-layout/layouts_demonstrated/),

We also set the type of the carousel to "slides" so that only one image shows at a time and included the `controls` attribute so that the user can see arrows to move between the slides. You can read more about the options for [`<amp-carousel>` in the documentation](https://amp.dev/documentation/components/amp-carousel/?format=email).

Run the script to send the email again:

```bash
npm run send
```

Return to your Gmail inbox and check out the latest email. This time you will see the dynamic email icon and you will find the dynamic carousel component.

![An animation of the result. Within the email there is a carousel where you can switch between two images.](https://twilio-cms-prod.s3.amazonaws.com/original_images/OEUCD4DECZThvcDJAvPn0O-9iM3ye67Tngb0j8BunlbQ7-I18wxclSaALyJio69nhyEIeTMdIFtPsU)

It might not be pretty, but you just sent an interactive email!

## You sent your first AMP email!

From here you can learn about the [other AMP components that you can use in your email](https://amp.dev/documentation/components/?format=email) and experiment with them in the [AMP playground](https://playground.amp.dev/?runtime=amp4email) without having to constantly send emails to yourself. You can [explore the repo with the completed project on GitHub](https://github.com/philnash/exploring-amp-email/) as well.

AMP emails open a treasure trove of interactions that your users can have with emails and have the potential to improve your user experience and conversion without your users leaving their inbox. You can build emails that have features like up-to-date delivery notifications, interactive polls and surveys, or comment threads. Anything you can think of that can be done with dynamic data and form submissions can live inside an email.

Once you are happy with your AMP emails you can [register for sender distribution](https://amp.dev/documentation/guides-and-tutorials/start/email_sender_distribution/?format=email) and start sending dynamic emails to your users.

I'd love to hear what you're excited about building with AMP Email, let me know in the comments or on Twitter at [@philnash](https://twitter.com/philnash).