Skip to content

Instantly share code, notes, and snippets.

@tedigc
Created June 16, 2018 00:09
Show Gist options
  • Select an option

  • Save tedigc/70c2e521a0ec44164369fbd79060609c to your computer and use it in GitHub Desktop.

Select an option

Save tedigc/70c2e521a0ec44164369fbd79060609c to your computer and use it in GitHub Desktop.
Python snippet for sending emails with Gmail
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