python - 如何从Python发送AMP电子邮件?它在技术上与普通电子邮件有何不同

标签 python email amp-email

我试图了解什么是 AMP 电子邮件,并了解如何从 Pyhton/NodeJs/Ruby 等方式发送它。

目前在 Python 中我发送电子邮件如下:



import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8cedac7c5e8cdc5c9c1c486c9ccccdacddbdb" rel="noreferrer noopener nofollow">[email protected]</a>"
to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="493d26092c2428202567282d2d3b2c3a3a" rel="noreferrer noopener nofollow">[email protected]</a>"

msg = MIMEMultipart('alternative')
msg['Subject'] = "AMP Email"
msg['From'] = from
msg['To'] = to

#Email body.
plain_text = "Hi,\nThis is the plain text version of this Email.\nHere is a link that is for testing:\nhttps://amp.dev/documentation/guides-and-tutorials/start/create_email/?format=email"
html_amp = """\
<html amp4email>
  <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>
    <p>Hi!<br>
    <h1>Hello, I am an AMP EMAIL!</h1>
    </p>
  </body>
</html>
"""

part1 = MIMEText(plain_text, 'plain')
part2 = MIMEText(html_amp, 'html')

msg.attach(part1)
msg.attach(part2)

s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()

但是上述方法不起作用。

我试图理解:

  1. AMP 为电子邮件带来的主要优势是什么?
  2. 技术上有何不同?
  3. 每个人都可以发送 AMP 邮件吗?
  4. 它与普通电子邮件有什么不同吗?

最佳答案

我想你已经快到了。您似乎还没有完全了解 AMP 电子邮件是什么。您的代码的快速更正版本如下所示,您已在 html mimetype 中给出了 AMP 内容:

#Main Mimetype
msg = MIMEMultipart('alternative')
msg['Subject'] = "AMP Email"
msg['From'] = from
msg['To'] = to

#Email body.
plain_text = "Hi,\nThis is the plain text version of this Email.\nHere is a link that is for testing:\nhttps://amp.dev/documentation/guides-and-tutorials/start/create_email/?format=email"


html = """\
<html>
  <head>
  <meta charset="utf-8">    
</head>
  <body>
    <p>Hi!<br>
    <h1>Hello, I am an HTML MAIL!</h1>
    </p>
  </body>
</html>
"""


html_amp = """\
<html amp4email>
  <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>
    <p>Hi!<br>
    <h1>Hello, I am an AMP EMAIL!</h1>
    </p>
  </body>
</html>
"""

#Important: Some email clients only render the last MIME part, so it is
#recommended to place the text/x-amp-html MIME part before the text/html.
part1 = MIMEText(plain_text, 'plain')
part2 = MIMEText(html_amp, 'x-amp-html')
part3 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)
msg.attach(part3)

s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()

回答您的问题:

简而言之:

AMP for Email 允许电子邮件发件人在其电子邮件中使用 AMP 来支持许多新功能。 AMP 电子邮件可以包含交互元素,例如图像轮播、通过 API 更新的联系人以及无需离开收件箱即可提交表单的功能。

与 HTML 电子邮件的技术差异:

AMP 电子邮件只是普通 HTML 电子邮件的扩展,它是多部分 MIME 消息。即使您可能不知道,您在 Gmail、Outlook 等上发送或接收的大多数电子邮件都是多部分 MIME 消息。这意味着,电子邮件由多个部分组成。通常是文本部分和 HTML 部分。

支持:

大多数基于桌面和网络的电子邮件阅读器都支持 HTML,并显示多部分消息的 HTML 部分。但是,某些移动阅读器可能会受到限制,仅显示多部分 MIME 消息的文本部分。随着AMP的出现,现在又多了一个部分,那就是AMP部分。能够支持 AMP 的电子邮件阅读器将选择该部分,其余部分将回退到 HTML 版本。 Gmail、Outlook、Yahoo 和 Mail.ru 已宣布支持 AMP 电子邮件。

示例: (AMP 电子邮件示例如下所示)

From:  Person A <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5958097968a8b84a5809d8488958980cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>>
To: Person B <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbbbaeb9b8a4a5a98baeb3aaa6bba7aee5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>>
Subject: An AMP email!
Content-Type: multipart/alternative; boundary="001a114634ac3555ae05525685ae"

--001a114634ac3555ae05525685ae
Content-Type: text/plain; charset="UTF-8"; format=flowed; delsp=yes

Hello World in plain text!

--001a114634ac3555ae05525685ae
Content-Type: text/x-amp-html; charset="UTF-8"

<!doctype html>
<html ⚡4email>
<head>
  <meta charset="utf-8">
  <style amp4email-boilerplate>body{visibility:hidden}</style>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body>
Hello World in AMP!
</body>
</html>
--001a114634ac3555ae05525685ae--
Content-Type: text/html; charset="UTF-8"

<span>Hello World in HTML!</span>
--001a114634ac3555ae05525685ae

    
    

要记住的要点:

  1. 某些电子邮件客户端只会呈现最后的 MIME 部分。将 text/x-amp-html MIME 部分放在 text/html MIME 部分之前。
  2. 由于许多客户不支持 AMP,因此 Html 后备内容是强制性的。除此之外,转发电子邮件时,AMP 部分会被删除。
  3. AMP 电子邮件只能从列入白名单的电子邮件发送。您可以按照本文档进行操作。 https://developers.google.com/gmail/ampemail/register

关于python - 如何从Python发送AMP电子邮件?它在技术上与普通电子邮件有何不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59321824/

相关文章:

ruby-on-rails - 在 Ruby on Rails 中开发 AMP 电子邮件的问题

python - 为什么 int(string) 给出值错误

python - 我真的改变了列表吗?

html - IIS 405 用于访问此页面的 HTTP 动词不允许电子邮件

php - 为什么此邮件消息不能正确解码?

python - 如何在二维数组中找到最接近的匹配字符串对

python - pandas:连接字符串行直到特定字符

c# - AjaxControlToolKit 图像在电子邮件中显示为 html 标记

ruby-on-rails - 如何在 ruby​​ on rails 中设置像 amp-email 这样的动态电子邮件?