Created
November 4, 2019 22:29
-
-
Save fstanis/c59c4f1dbdde710d15f3863e1cf6bef0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Copyright 2019 Google LLC. | |
# SPDX-License-Identifier: Apache-2.0 | |
# Sends an AMP email from a Gmail account. | |
import smtplib | |
from email import encoders | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
SUBJECT = 'AMP test email' | |
RECIPIENT = '' | |
SMTP_SERVER = 'smtp.gmail.com:587' | |
SMTP_USERNAME = '[email protected]' | |
SMTP_PASSWORD = '' | |
def CreateAMPEmail(): | |
amp = MIMEText('<!doctype html><html amp4email><head><meta charset="utf-8"><script async src="https://cdn.ampproject.org/v0.js"></script><style amp4email-boilerplate>body{visibility:hidden}</style></head><body>Hello, AMP</body></html>', 'x-amp-html') | |
encoders.encode_quopri(amp) | |
del amp['Content-Transfer-Encoding'] | |
amp.add_header('Content-Transfer-Encoding', 'quoted-printable') | |
payload = MIMEMultipart('alternative') | |
payload['Subject'] = SUBJECT | |
payload['From'] = SMTP_USERNAME | |
payload['To'] = RECIPIENT | |
payload.attach(MIMEText('Hello, plain text', 'plain')) | |
payload.attach(amp) | |
payload.attach(MIMEText('Hello, <b>HTML</b>', 'html')) | |
return payload | |
def SendEmailViaSMTP(email): | |
smtp = smtplib.SMTP(SMTP_SERVER) | |
try: | |
smtp.ehlo() | |
smtp.starttls() | |
smtp.login(SMTP_USERNAME, SMTP_PASSWORD) | |
smtp.sendmail(SMTP_USERNAME, email['To'], email.as_string()) | |
except Exception as e: | |
print('SMTP Exception:\n' + str(e)) | |
finally: | |
smtp.quit() | |
def main(): | |
email = CreateAMPEmail() | |
SendEmailViaSMTP(email) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment