Skip to content

Instantly share code, notes, and snippets.

@adeel
Created August 4, 2009 03:20
Show Gist options
  • Save adeel/161000 to your computer and use it in GitHub Desktop.
Save adeel/161000 to your computer and use it in GitHub Desktop.
simple smtplib wrapper
import smtplib
from smtplib import SMTPException
from email.mime.text import MIMEText
config = {
'host': None,
'port': None,
'username': None,
'password': None,
'use_tls': False,
'use_ssl': False,
}
def send(sender, recipients, subject, message, format='plain'):
"""
sender: an email address
recipients: a list of email addresses
subject: a string
message: a string
format: 'plain' or 'html'
"""
if not config.get('use_ssl'):
server = smtplib.SMTP(config.get('host'), config.get('port'))
else:
server = smtplib.SMTP_SSL(config.get('host'), config.get('port'))
if config.get('use_tls'):
server.ehlo()
server.starttls()
server.ehlo()
if config.get('username'):
server.login(config.get('username'), config.get('password'))
msg = MIMEText(message, format)
msg['From'] = sender
msg['To'] = ', '.join(recipients)
msg['Subject'] = subject
server.sendmail(sender, recipients, msg.as_string())
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment