Skip to content

Instantly share code, notes, and snippets.

@shapiromatron
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save shapiromatron/1700c76e316b88a57dca to your computer and use it in GitHub Desktop.

Select an option

Save shapiromatron/1700c76e316b88a57dca to your computer and use it in GitHub Desktop.
send email using gmail and built-in python smtplib
import smtplib
def send_email(email, pw, subject, text):
FROM = email
TO = [email]
try:
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s\n""" % (FROM, ", ".join(TO), subject, text)
server = smtplib.SMTP("smtp.gmail.com", 587) # alternatively port 465
server.ehlo()
server.starttls()
server.login(email, pw)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
if __name__ == "__main__":
email = "swiss_cheese@gmail.com"
pw = "emmental "
send_email(email, pw, "testing", "here we go...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment