HTML based emails from Python (ssmtp)

In this post I provide a simple Python code which utilizes ssmtp with MIME in order to send an HTML based email with an embedded image.

Recently I needed to create an email similar to the one mentioned in a previous post- how to send HTML based emails from SAP DataServices . Only this time it needed to be done in Python with ssmtp.

The code below is a simple example on how this can be achieved –
It processes one message at a time (from a SQL cursor), constructing MIME code which includes HTML followed by binary data for the embedded image file (the binary data is encoded with uuencode). The MIME code parts are separated by “–border” in order to tell them apart.

import psycopg2
import subprocess

#Define Redshift connection
redshift = psycopg2.connect("dbname=<DB> host=<server> port=5439 user=<user> password=<pass>")

#Import messages to process based on SQL output
cursor = redshift.cursor()
cursor.execute("SELECT MSG_TO, MSG_SUBJECT, MSG_ID, MSG_CONTENT FROM MESSAGES")
result = cursor.fetchall()

#Process each message
for row in result:
    MsgTo = row[0]
    MsgSub = row[1]
    MsgId = int(row[2])
    MsgContent = row[3]

    #Create MIME message code
    MessageCode = """To: %s
Subject: %s
Mime-Version: 1.0;
Content-Type: multipart/related; boundary=border;
--border
Content-Type: text/html; charset=UTF-8;

<html>
<body>
    	<table border="1" id="vertical-1">
        <caption>Email ID: %s</caption>
            <tr>
            	<th>Content</th>
                <td>%s</td>
            </tr>
	</table>
        <img src="cid:logo.png">
</body> 	
</html>
--border
Content-Type: image/png;
Content-Transfer-Encoding: uuencode;

""" % (MsgTo,MsgSub,MsgId,MsgContent)
    #Execute ssmtp with MIME code
    command = 'true | (cat - && echo "%s" && uuencode <path>/source_image.png logo.png && echo -e "\n--border--") | ssmtp %s' % (MessageCode,MsgTo)
    cmd = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    out, err = cmd.communicate()
    print (out)
    print (err)

 

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 )

Connecting to %s