Skip to content

Instantly share code, notes, and snippets.

@bradwright
Created November 11, 2009 14:29
Show Gist options
  • Save bradwright/231983 to your computer and use it in GitHub Desktop.
Save bradwright/231983 to your computer and use it in GitHub Desktop.
from email.utils import formatdate
from email.message import Message
from email.mime.text import MIMEText
import smtplib
import textwrap
def send_email(to_address, to_name, from_address, from_name, subject, body, **kwargs):
msg = MIMEText(body)
msg['To'] = '%s <%s>' % (to_name, to_address)
msg['From'] = '%s <%s>' % (from_name, from_address)
msg['Reply-To'] = '%s <%s>' % (kwargs.get('reply_to_name', from_name), kwargs.get('reply_to_email', from_address))
msg['Subject'] = subject
msg['Date'] = formatdate()
to_emails = [to_address]
bcc_emails = kwargs.get('bcc_emails')
if bcc_emails:
to_emails.extend(bcc_emails)
server = smtplib.SMTP(settings.EMAIL_HOST)
# make sure we don't double up on email addresses
to_emails = list(set(to_emails))
server.sendmail(from_address, to_emails, msg.as_string())
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment