How to send AMP Email from Python? How is it technically different from Normal Email

后端 未结 1 693
有刺的猬
有刺的猬 2021-02-11 05:34

I am trying to understand what is AMP Email and also understand how I can send it from something like Pyhton/NodeJs/Ruby.

Currently in Python I send email as below:

相关标签:
1条回答
  • 2021-02-11 06:07

    I think you are almost there. You seem to not have fully understand what AMP Email is. A quick corrected version of your code will be as below, you have given AMP content in html mimetype:

    #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()
    

    To answer your questions:

    In-Short:

    
AMP for Email lets email senders use AMP in their email messages to support many new features. AMP Email can contain interactive elements, like image carousels, updated contact via API, and the ability to submit a form without leaving the inbox.



    Technical Difference from HTML Email:

    
AMP email is just an extension of a normal HTML email, which is a multipart MIME message. Most of the email you send or receive on Gmail, Outlook etc are multipart MIME messages, even though you may not be aware. It means, the email consists of multiple parts. Usually a text part and an HTML part.

    Support:

    Most desktop & web based email readers are HTML-capable and display the HTML part of the multipart message. However, some mobile readers might be limited and display only the text part of the multipart MIME message. With the advent of AMP, now’ll have one more part that is the AMP part. Email readers that has capability to support AMP will pick that part and rest will fall back to the HTML version. Gmail, Outlook, Yahoo & Mail.ru has already announced support for AMP Email.

    Example: (An example AMP Email will look as below)

    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
    
    
    
    

    Key points to keep in mind:

    1. Some email clients will only render the last MIME part. Place the text/x-amp-html MIME part _before _the text/html MIME part.
    2. A Html fallback content is compulsory as many clients don’t support AMP. Above that, AMP part is removed when the email is forwarded.
    3. AMP email can only be send from a whitelisted email. You can do that by following this documentation. https://developers.google.com/gmail/ampemail/register
    0 讨论(0)
提交回复
热议问题