Skip to content

Instantly share code, notes, and snippets.

@AaronTraas
Last active August 29, 2015 14:23
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 AaronTraas/6fdab15d66cb7b09d4c3 to your computer and use it in GitHub Desktop.
Save AaronTraas/6fdab15d66cb7b09d4c3 to your computer and use it in GitHub Desktop.
[Python] test email credentials
#!/usr/bin/python
from smtplib import SMTP
from email.MIMEText import MIMEText
SERVER = "<SMTP_server>"
USERNAME = "<SMTP_user_name>"
PASSWORD = "<SMTP_password>"
DESTINATION = "<destination_email_address>"
try:
msg = MIMEText('Test message', 'plain')
msg['Subject']= "Testing credentials"
msg['From'] = USERNAME
conn = SMTP(SERVER)
conn.set_debuglevel(1)
conn.starttls()
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(USERNAME, DESTINATION, msg.as_string())
finally:
conn.close()
except Exception, exc:
sys.exit( "Sending mail failed: %s" % str(exc) ) # give a error message
@AaronTraas
Copy link
Author

I find this useful to verify SMTP credentials sent to me by a client.

Modified from code found here: http://stackoverflow.com/questions/64505/sending-mail-from-python-using-smtp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment