Skip to content

Instantly share code, notes, and snippets.

@jaywink
Created November 29, 2012 10:45
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 jaywink/4168148 to your computer and use it in GitHub Desktop.
Save jaywink/4168148 to your computer and use it in GitHub Desktop.
Test SMTP sending with Python ... found the template on some blog
#! /usr/local/bin/python
SMTPserver = 'smtp.domain.com'
sender = 'email@smtp.domain.com'
destination = ['recipient@domain.com']
USERNAME = "username4smtpserver"
PASSWORD = "password4smtpserver"
# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'
content="""\
Test message
"""
subject="Sent from Python"
import sys
import os
import re
from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
from email.MIMEText import MIMEText
try:
msg = MIMEText(content, text_subtype)
msg['Subject']= subject
msg['From'] = sender # some SMTP servers will do this automatically, not all
conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.close()
except Exception, exc:
sys.exit( "mail failed; %s" % str(exc) ) # give a error message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment