Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues developing AMP email in Ruby on Rails

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"
 },
like image 969
chhhris Avatar asked Feb 07 '20 01:02

chhhris


1 Answers

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.

like image 181
Lalu Avatar answered Oct 20 '22 03:10

Lalu