Skip to content

Instantly share code, notes, and snippets.

@aseeon
Created July 11, 2009 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aseeon/145285 to your computer and use it in GitHub Desktop.
Save aseeon/145285 to your computer and use it in GitHub Desktop.
Sending Email from Python scripts using Gmail account
#!/usr/bin/python
# Thanks to Hal Otis
# http://www.halotis.com/2009/07/11/sending-email-from-python-using-gmail/
# for the script!
import smtplib
from email.MIMEText import MIMEText
GMAIL_LOGIN = 'myemail@gmail.com'
GMAIL_PASSWORD = 'password'
def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=GMAIL_LOGIN):
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login(GMAIL_LOGIN,GMAIL_PASSWORD)
server.sendmail(from_addr, to_addr, msg.as_string())
server.close()
if __name__=="__main__":
send_email('test', 'This is a test email')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment