Created
August 4, 2009 03:20
-
-
Save adeel/161000 to your computer and use it in GitHub Desktop.
simple smtplib wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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