Skip to main content
 首页 » 编程设计

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

2024年08月12日44三少

我想了解什么是 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() 

但是上面的方法是行不通的。

我正在努力理解:

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

请您参考如下方法:

我想你快到了。您似乎还没有完全理解 AMP Email 是什么。您的代码的快速更正版本如下所示,您在 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 <persona@example.com> 
To: Person B <personb@example.com> 
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. Html 后备内容是强制性的,因为许多客户不支持 AMP。除此之外,转发电子邮件时会删除 AMP 部分。
  3. AMP 电子邮件只能从列入白名单的电子邮件中发送。您可以按照本文档执行此操作。 https://developers.google.com/gmail/ampemail/register