chh*_*ris 6 ruby ruby-on-rails actionmailer amp-html amp-email
我正在尝试(但失败)使用 ActionMailer 配置 Ruby on Rails 应用程序以发送 AMP 电子邮件。现在正在寻找有关如何进一步调试的任何建议,我不知道还能做什么!
示例 AMP 模板在从AMP Gmail 游乐场发送时有效,但是当我从我们的 Rails 应用程序发送示例时,AMP 版本不会在 Gmail 中呈现。
在config/initializers/mime_types.rb我补充说:
Mime::Type.register 'text/x-amp-html', :amp
Run Code Online (Sandbox Code Playgroud)
AMP 标记位于名为app/views/reminder_mailer/foo_notification.amp.erb. 为了测试,我的邮件程序方法如下所示:
def foo_notification
mail(to: 'foo@example.com', subject: 'Foo subject') do |format|
format.amp
format.text
format.html
end
end
Run Code Online (Sandbox Code Playgroud)
我的 Rails 控制台的输出显示了正确发送的邮件,Content-Type: multipart/alternative后跟Content-Type: text/x-amp-html. 完整输出如下。
ReminderMailer#foo_notification: processed outbound mail in 19.9ms
Sent mail to foo@example.com (625.2ms)
Date: Thu, 06 Feb 2020 16:47:56 -0800
From: example <notifier@example.com>
Reply-To: example <notifier@example.com>
To: foo@example.com
Message-ID: <5e3cb3bc74b91_2cd13ff4e08417cc34068@Chris-MacBook-Pro.local.mail>
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--
Run Code Online (Sandbox Code Playgroud)
最后,我使用 Gmail 的 API 来检查完整的邮件内容。成功的 AMP playground 与失败的 Rails AMP 之间存在一些差异。例如,两者的值"name": "ARC-Authentication-Results"显示不同。此外,AMP 游乐场电子邮件包含以下属性,这些属性不在失败的 AMP 电子邮件中:
{
"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"
},
Run Code Online (Sandbox Code Playgroud)
AMP 电子邮件仅在通过 DKIM 和 SPF 身份验证后才有效。因此,您需要一个有效的域和一个在服务器上运行的应用程序。这些对于工作来说是绝对必要的。这意味着您无法在本地主机中测试它(至少它对我不起作用)。
ApplicationMailer 中另一个值得注意的设置是像这样设置 :parts_order :
default from: "abcd@example.com",
parts_order: [ 'text/plain', 'text/enriched', 'text/x-amp-html', 'text/html' ]
Run Code Online (Sandbox Code Playgroud)
如果parts_order没有这样设置,那么一些电子邮件客户端(如Mail、Outlook)将在输出中显示amp标签,默认情况下显示最后一封电子邮件。
我在此处创建了一篇关于 Ruby on Rails 中的 AMP 电子邮件的博客文章。