Issues developing AMP email in Ruby on Rails
Asked Answered
C

1

6

I am trying (and failing) to configure a Ruby on Rails app with ActionMailer to send an AMP email. Looking for any advice on how to further debug as right now I don't know what else to do!

The sample AMP template works when sent from AMP Gmail playground however when I send the sample from our Rails app the AMP version is not rendered in Gmail.

In config/initializers/mime_types.rb I added:

Mime::Type.register 'text/x-amp-html', :amp

The AMP markup is in a file called app/views/reminder_mailer/foo_notification.amp.erb. For testing, my mailer method looks like:

def foo_notification
  mail(to: '[email protected]', subject: 'Foo subject') do |format|
    format.amp
    format.text
    format.html
  end
end

The output from my Rails console shows the mail correctly sent with Content-Type: multipart/alternative followed by Content-Type: text/x-amp-html. The full output follows below.

ReminderMailer#foo_notification: processed outbound mail in 19.9ms

Sent mail to [email protected] (625.2ms)
Date: Thu, 06 Feb 2020 16:47:56 -0800
From: example <[email protected]>
Reply-To: example <[email protected]>
To: [email protected]
Message-ID: <[email protected]>
Subject: Test AMP email 62
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5e3cb3bc73df1_2cd13ff4e08417cc339b3";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_5e3cb3bc73df1_2cd13ff4e08417cc339b3
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Plain text.
----==_mimepart_5e3cb3bc73df1_2cd13ff4e08417cc339b3
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<h1>Foo HTML content</h1>
<div>Hey yo this is the HTML.</div>
----==_mimepart_5e3cb3bc73df1_2cd13ff4e08417cc339b3
Content-Type: text/x-amp-html;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<!--=0D
     Below is the mininum valid AMP4EMAIL document. Just type away=0D
     here and the AMP Validator will re-check your document on the fly.=0D=

-->=0D
<!doctype html>=0D
<html =E2=9A=A14email>=0D
<head>=0D
  <meta charset=3D"utf-8">=0D
  <script async src=3D"https://cdn.ampproject.org/v0.js"></script>=0D
  <style amp4email-boilerplate>body{visibility:hidden}</style>=0D
</head>=0D
<body>=0D
  Hello, AMP4EMAIL world.=0D
</body>=0D
</html>=

----==_mimepart_5e3cb3bc73df1_2cd13ff4e08417cc339b3--

Finally, I used Gmail's API to inspect the full message contents. There are a few differences between the successful AMP playground vs the failed AMP from Rails. For example, the value for "name": "ARC-Authentication-Results" shows differently between the two. In addition, the AMP playground email included these attributes which were not in the failed AMP emails:

{
  "name": "X-Google-DKIM-Signature",
  "value": ...
 },
 {
  "name": "X-Gm-Message-State",
  "value": ...
 },
 {
  "name": "X-Google-Smtp-Source",
  "value": ...
 },
 {
  "name": "X-Received",
  "value": ...
 },
 {
  "name": "X-Google-Appengine-App-Id",
  "value": "s~dynamic-mail-playground"
 },
 {
  "name": "X-Google-Appengine-App-Id-Alias",
  "value": "dynamic-mail-playground"
 },
Complexioned answered 7/2, 2020 at 1:25 Comment(4)
Have you complied with Google's delivery requirements for AMP email? developers.google.com/gmail/ampemail/…Foregut
@AndrewSinner helpful to point that out, will check against each of those criteria and update the post if I find anything.Complexioned
Following up to confirm that, as far as I can tell, we do comply with Google's delivery requirements for AMP email. cc @AndrewSinnerComplexioned
Update: our DKIM is not passing.Complexioned
L
3

AMP emails only work after passing DKIM and SPF authentication. So you need a valid domain and an application running on a server. Those are absolute necessary to work. Which means you cannot test it in your localhost (At least it didn't work for me).

One more setting worth noting in ApplicationMailer is to set :parts_order like this:

default from: "[email protected]",
        parts_order: [ 'text/plain', 'text/enriched', 'text/x-amp-html', 'text/html' ]

If parts_order is not set like this, then some email clients like Mail, Outlook will display amp tags in the output, which by default display last email piece.

I created a blog post about AMP Emails in Ruby on Rails here.

Lon answered 8/2, 2020 at 2:29 Comment(6)
I read your Medium post, it was excellent. I still am unable to get AMP emails to render. Checked all the following: SPF/DKIM/DMARC are PASS. API is SendGrid. Sent from remote server. Sender email is whitelisted in Gmail settings>Dynamic email. Content: multipart/alternative. Correct parts order. This is the full msg - gist.github.com/chhhris/bdf40c177b9f5cee5ff699bee48e447c. Do you have any ideas what else I should check!? (Edit: the email is delivered as HTML, just no AMP.)Complexioned
Hi @Complexioned - Checked your gist file. Why does it says - Delivered-To: [email protected]. Can you try with a Gmail address? Everything else looks good.Lon
Hey @Lon it's the same with gmail, AMP still not rendering - gist.github.com/chhhris/c43861b01a1b7ad96426cf59fae78e01. In other news I emailed the AMP team at [email protected] they said they would look at it and let me know why it's falling back to the HTML. Appreciate your help here!Complexioned
@Complexioned Looking at your email payload, the email must've failed AMP validation because of the use of HTTP URL in the amp-img src attribute: http://url3308.copper.com/wf/open?.... Only HTTPS image URLs are allowed. You can test whether an email delivered to your inbox passes AMP validation by downloading the original email as an .eml file (Click on the per-message menu -> "Show original" -> "Download original"), and then click on "IMPORT EMAIL" at playground.amp.dev/?runtime=amp4email to decode the AMP MIME part and check for validation.Railing
@SuZhang thanks so much for taking a look and the tip about export the *.eml file into the playground -- I updated the email and exported it, it now completely validates on the playground, yet GMail still does not display the AMP portion!! Updates I made: that URL was a pixel tracker inserted by SendGrid, I disabled the feature. I also had to monkey patch Rails Action Mailer mail() method to fix some syntax with the header that AMP did not like. Not sure why a completely valid *.eml file is not rendering AMP... 🤔Complexioned
Update for @SuZhang it is working now! Issue was I had whitelisted a different sender while I sorted out some SPF/DKIM/DMARC issues with our primary domain. Thank you very much!!Complexioned

© 2022 - 2024 — McMap. All rights reserved.