Created
June 16, 2018 00:09
-
-
Save tedigc/70c2e521a0ec44164369fbd79060609c to your computer and use it in GitHub Desktop.
Python snippet for sending emails 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
| import smtplib | |
| from email.parser import Parser | |
| from email.mime.multipart import MIMEMultipart | |
| from email.mime.text import MIMEText | |
| USR = "<gmail_username>" | |
| PWD = "<gmail_app_password>" | |
| # Set up server | |
| server = smtplib.SMTP("smtp.gmail.com", 587) | |
| server.ehlo() | |
| server.starttls() | |
| server.login(USR, PWD) | |
| # Set up sender, recipient, and subject | |
| msg = MIMEMultipart() | |
| msg['From'] = "<sender_email_address>" | |
| msg['To'] = "<recipient_email_address>" | |
| msg['Subject'] = "<subject_line>" | |
| # Attach body to email | |
| text = MIMEText(open("body.txt").read()) | |
| msg.attach(text) | |
| # Send and close | |
| server.send_message(msg) | |
| server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment