Created
December 15, 2017 13:56
-
-
Save walkingmask/33a4cea9177d0050a57727c28b96cb66 to your computer and use it in GitHub Desktop.
A example of sending email with gmail.
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| from email.mime.text import MIMEText | |
| from email.utils import formatdate | |
| import smtplib | |
| addr = "" | |
| pswd = "" | |
| subj = "" | |
| body = "" | |
| msg = MIMEText(body) | |
| msg['Subject'] = subj | |
| msg['From'] = addr | |
| msg['To'] = addr | |
| msg['Date'] = formatdate() | |
| try: | |
| with smtplib.SMTP('smtp.gmail.com', 587) as smtps: | |
| smtps.ehlo() | |
| smtps.starttls() | |
| smtps.ehlo() | |
| smtps.login(addr, pswd) | |
| smtps.sendmail(addr, addr, msg.as_string()) | |
| smtps.close() | |
| print("Successfully sent email") | |
| except Exception as e: | |
| print("Error: unable to send email") | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment