Skip to content

Instantly share code, notes, and snippets.

@atvKumar
Created March 6, 2014 06:10
Show Gist options
  • Save atvKumar/9383424 to your computer and use it in GitHub Desktop.
Save atvKumar/9383424 to your computer and use it in GitHub Desktop.
Simple Send Text Email from Python using Gmail
from smtplib import SMTPException
from email.utils import COMMASPACE
import smtplib
_default_address = ['[email protected]']
def SendGmail(subject, body, to_addresses=_default_address,
from_address='[email protected]', debug=False):
email_header = "From: My Server <[email protected]>\n" \
"To: {0}\n" \
"MIME-Version: 1.0\n" \
"Content-type: text/html\n" \
"Subject: {1}\n"
email_header = email_header.format(COMMASPACE.join(to_addresses), subject)
email_message = email_header + '\n\n {0} \n\n'.format(body)
if not debug:
try:
session = smtplib.SMTP(host='smtp.gmail.com', port=587)
session.ehlo()
session.starttls()
session.login('gmailUserName', 'gmailUserPassword')
session.sendmail(from_address, to_addresses, email_message)
session.quit()
print "Successfully sent email"
return True
except SMTPException:
print "Error: unable to send email"
return False
else:
print email_message
SendGmail('QSN Server Notification!', 'GA0001 Created')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment