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

Far*_*oth 2 python email amp-email

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

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



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

from = "from@email.address"
to = "to@email.address"

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()
Run Code Online (Sandbox Code Playgroud)

然而上述方法不起作用。

我试图理解:

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

esa*_*wan 5

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

\n\n
#Main Mimetype\nmsg = MIMEMultipart(\'alternative\')\nmsg[\'Subject\'] = "AMP Email"\nmsg[\'From\'] = from\nmsg[\'To\'] = to\n\n#Email body.\nplain_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"\n\n\nhtml = """\\\n<html>\n  <head>\n  <meta charset="utf-8">    \n</head>\n  <body>\n    <p>Hi!<br>\n    <h1>Hello, I am an HTML MAIL!</h1>\n    </p>\n  </body>\n</html>\n"""\n\n\nhtml_amp = """\\\n<html amp4email>\n  <head>\n  <meta charset="utf-8">\n  <script async src="https://cdn.ampproject.org/v0.js"></script>\n  <style amp4email-boilerplate>body{visibility:hidden}</style>\n  <style amp-custom>\n    h1 {\n      margin: 1rem;\n    }\n  </style>\n</head>\n  <body>\n    <p>Hi!<br>\n    <h1>Hello, I am an AMP EMAIL!</h1>\n    </p>\n  </body>\n</html>\n"""\n\n#Important: Some email clients only render the last MIME part, so it is\n#recommended to place the text/x-amp-html MIME part before the text/html.\npart1 = MIMEText(plain_text, \'plain\')\npart2 = MIMEText(html_amp, \'x-amp-html\')\npart3 = MIMEText(html, \'html\')\n\nmsg.attach(part1)\nmsg.attach(part2)\nmsg.attach(part3)\n\ns = smtplib.SMTP(\'localhost\')\ns.sendmail(me, you, msg.as_string())\ns.quit()\n
Run Code Online (Sandbox Code Playgroud)\n\n

回答您的问题:

\n\n

简而言之:

\n\n

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

\n\n

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

\n\n

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

\n\n

\xe2\x80\xa8支持:

\n\n

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

\n\n

\xe2\x80\xa8示例: \n(示例 AMP 电子邮件如下所示)

\n\n
From:  Person A <persona@example.com>\nTo: Person B <personb@example.com>\nSubject: An AMP email!\nContent-Type: multipart/alternative; boundary="001a114634ac3555ae05525685ae"\n\n--001a114634ac3555ae05525685ae\nContent-Type: text/plain; charset="UTF-8"; format=flowed; delsp=yes\n\nHello World in plain text!\n\n--001a114634ac3555ae05525685ae\nContent-Type: text/x-amp-html; charset="UTF-8"\n\n<!doctype html>\n<html \xe2\x9a\xa14email>\n<head>\n  <meta charset="utf-8">\n  <style amp4email-boilerplate>body{visibility:hidden}</style>\n  <script async src="https://cdn.ampproject.org/v0.js"></script>\n</head>\n<body>\nHello World in AMP!\n</body>\n</html>\n--001a114634ac3555ae05525685ae--\nContent-Type: text/html; charset="UTF-8"\n\n<span>Hello World in HTML!</span>\n--001a114634ac3555ae05525685ae\n\xe2\x80\xa8    \xe2\x80\xa8    \n
Run Code Online (Sandbox Code Playgroud)\n\n

要记住的要点:

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