Python smtplib – Send Plaintext and HTML emails

I had to do quite some research to implement email functionality with CC and BCC. Here’s ready to use code. Pass in a dictionary with to, cc, bcc, subject and body and function would do the rest.

#!/usr/bin/python

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def sendPlaintextMail(dict_params):
      email_subject = dict_params['email_subject']
      email_from = "from_email@domain.com"
      email_to = dict_params['email_to']
      email_cc = email_bcc = None
      if 'email_cc' in dict_params and dict_params['email_cc']:
            email_cc = dict_params['email_cc']
      if 'email_bcc' in dict_params and dict_params['email_bcc']:
            email_bcc = dict_params['email_bcc']
      email_body = dict_params['email_body']

      msg = MIMEMultipart('alternative')
      msg['To'] = email_to
      msg['CC'] = email_cc
      msg['Subject'] = email_subject
      mt_html = MIMEText(email_body, 'plain')
      msg.attach(mt_html)

      server = smtplib.SMTP('YOU_MAIL_SERVER.DOMAIN.COM')
      server.set_debuglevel(1)
      toaddrs = [email_to] + [email_cc] + [email_bcc]
      server.sendmail(email_from, toaddrs, msg.as_string())
      server.quit()

def sendHtmlMail(dict_params):
      email_subject = dict_params['email_subject']
      email_from = "from_email@domain.com"
      email_to = dict_params['email_to']
      email_cc = email_bcc = None
      if 'email_cc' in dict_params and dict_params['email_cc']:
            email_cc = dict_params['email_cc']
      if 'email_bcc' in dict_params and dict_params['email_bcc']:
            email_bcc = dict_params['email_bcc']
      email_body = dict_params['email_body']

      msg = MIMEMultipart('alternative')
      msg['To'] = email_to
      msg['CC'] = email_cc
      msg['Subject'] = email_subject
      mt_html = MIMEText(email_body, 'html')
      msg.attach(mt_html)

      server = smtplib.SMTP('YOU_MAIL_SERVER.DOMAIN.COM')
      server.set_debuglevel(1)
      toaddrs = [email_to] + [email_cc] + [email_bcc]
      server.sendmail(email_from, toaddrs, msg.as_string())
      server.quit()

# Calling the mailer functions
dict_params = {}
dict_params['email_to'] = 'to_email@domain.com'
dict_params['email_cc'] = 'cc_email@domain.com'
dict_params['email_bcc'] = 'bcc_email@domain.com'
dict_params['email_subject'] = 'Test message from python library'
dict_params['email_body'] = '<h1>Hello World</h1>'
sendPlaintextMail(dict_params)
sendHtmlMail(dict_params)

Now, if you need to send email to multiple recipients, the msg[‘To’] in the MIMEMultipart needs to be a comma separated string and the toaddrs needs to be a list. So here’s a slight modification that would be required to the above function in order to send it to multiple recipients (although this would work for a single recipient as well).

      .....
      msg = MIMEMultipart('alternative')
      msg['To'] = ",".email_to
      .....
# Calling the mailer functions
dict_params = {}
dict_params['email_to'] = ['to_email@domain.com', 'another_email@domain.com']
.....
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s