Skip to content

Instantly share code, notes, and snippets.

@luapz
Created January 18, 2013 13:56
Show Gist options
  • Save luapz/4564694 to your computer and use it in GitHub Desktop.
Save luapz/4564694 to your computer and use it in GitHub Desktop.
Sending Emails Via Gmail SMTP With Python
#!/usr/bin/python
import smtplib
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = '[email protected]'
recipient = '[email protected]'
subject = 'Gmail SMTP Test'
body = 'blah blah blah'
"Sends an e-mail to the specified recipient."
body = "" + body + ""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment