AMP email not getting decrypted at receiver's end.
I made a AMP email in amp playground and validated the code in amp validator and amp playground. The code passed in both case and I was able to sent a test amp email through amp playground.
So I wrote a python script to replicate the same using Gmail api. I setup a project in google cloud and downloaded t he credentials to the local system. the credentials were stored in credentials.json file and to capture the token created a blank file with name token.json. On running the script the authentication is dine without issue and mail is also sent but the received content is in encrypted formant and it is not getting decrypted at receivers end.
import os
import base64
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Scopes required for Gmail API access
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
# Path to the credentials file downloaded from Google Cloud Console
CREDENTIALS_FILE = os.path.expanduser('path of credentials.json file')
# Path to the token file generated after authorization
TOKEN_FILE = os.path.expanduser('path of token.json file')
# Email content
amp_html = """
<!doctype html>
<html ⚡4email data-css-strict>
<head>
<meta charset="utf-8">
<style amp4email-boilerplate>body{visibility:hidden}</style>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
</head>
<body>Hello, <br> Please enter the details for referrals.<br>
<form method="post" action-xhr="address of webhook">
<label for="startup-name">Startup Name:</label>
<input type="text" id="startup-name" name="startupName" required>
<br>
<label for="founder-name">Founder Name:</label>
<input type="text" id="founder-name" name="founderName" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
"""
def create_amp_message(sender, receiver, subject, amp_html):
message = MIMEMultipart("alternative")
message["to"] = receiver
message["from"] = sender
message["subject"] = subject
amp_part = MIMEText(amp_html, "html", "utf-8")
amp_part.replace_header("Content-Type", "text/html; charset=utf-8")
amp_part.replace_header("Content-Transfer-Encoding", "quoted-printable")
amp_part.add_header("Content-Disposition", "inline")
message.attach(amp_part)
raw_message = base64.b64encode(message.as_bytes()).decode()
return {"raw": raw_message}
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_FILE, SCOPES)
creds = flow.run_local_server(port=8080)
creds_file_content = creds.to_json()
with open(TOKEN_FILE, 'w') as token:
token.write(creds_file_content)
return build('gmail', 'v1', credentials=creds)
def send_email(service, user_id, message):
try:
message = service.users().messages().send(userId=user_id, body=message).execute()
print('AMP email sent successfully! Message ID: %s' % message['id'])
except Exception as e:
print('An error occurred while sending the email: %s' % str(e))
def main():
sender_email = "sender email id"
receiver_email = "receiver email id"
subject = "AMP Email using Python"
amp_message = create_amp_message(sender_email, receiver_email, subject, amp_html)
service = get_authenticated_service()
send_email(service, 'me', amp_message)
if __name__ == '__main__':
main()
I tried validating the amp email body with amp validator and amp-playground both case the code passed the validation. The I was also able to send a test email with the same code from amp playground.
I wrote the python script to replicate the same from my local machine so that I can give it to google for whitelisting. But the content received at the receiver's end is in an encrypted form and it is not getting decrypted. I tried sent it to different mail servers like Gmail and Yandex both case same issue.
A couple of observations:
You are including AMP HTML in a
text/htmlMIME part. You are supposed to include AMP HTML in atext/x-amp-htmlMIME part and include the fallback static HTML email in thetext/htmlMIME part. You need to have both the AMP MIME part and a fallback MIME part (either HTML or plain text).You specified
quoted-printableas the encoding scheme, but it's unclear whether your script is properly encoding your email usingquoted-printable(I'm not familiar with the libraries you used).The script seems to be sending emails to
mewhich is an alias for sending emails to self in the Gmail API. If you are testing AMP emails in Gmail, you need to ensure that the sender is not also yourself because that's not supported:https://developers.google.com/gmail/ampemail/testing-dynamic-email#delivery_requirements