How to Email MRTG Graphs using Python SMTP Library & Gmail

How to Email MRTG Graphs using Python SMTP Library and Gmail

Send mail with PythonOne of the easiest ways I have found to create automated email messages is to use the Python SMTP Library. The code is pretty straightforward and allows you to send attachments. Using a gmail account, allows email to work whether you have a local SMTP server installed and running or not (more portable).

On my last project, I am using MRTG to graph some traffic OID’s, but the server does not have Apache installed, and even if it did, it’s firewalled with no access to Port 80. To compound matters, I do not have control over the server, as it belongs to the customer.

How to get those pretty graphs out to the rest of the world? 

I investigated several ways of removing the data from the server, TFTP, FTP, SCP, etc. the goal was to email the graphs to the interested parties, so why not just email directly from the server? The files are already there, and MRTG creates the HTML for me on the fly.

Python SMTP Library

Luckily, we do have Python installed on this server that is being used for some other tasks, so I decided to use the SMTP Library. I’ve used this library in past projects to do email attachments, so I was familiar with how to use it. What I had not done, was change the message body to HTML.

I found an example on StackOverflow and used it as the basis to start coding. If you just import the MRTG generated HTML file, the email will come through with attachments, but it won’t display them properly in the email client. What I wanted, was an exact duplicate of what would be seen on a webpage.

green

Recycling Code (can we call this green code?)

I went back to another project to look at how the images were attached.

# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

# Base path for image files
basepath = '/home/gifs'

# Define these once; use them twice!
strFrom = 'user@gmail.com'
strTo = ['user1@yourcompany.com', 'user2@yourcompany.com']
strSubject = 'Your Important Message'

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = strSubject
msgRoot['From'] = strFrom
msgRoot['To'] = ''.join(strTo)
msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('Important graphic here!<br><img src="cid:image1"><br>Another important graphic here!<br><img src="cid:image2"><br>', 'html')
msgAlternative.attach(msgText)

# Use the basepath directory to find the gif files
fp = open('%s/image1.gif' % basepath, 'rb')
msgImage1 = MIMEImage(fp.read())
fp.close()
fp = open('%s/image2.gif' % basepath, 'rb')
msgImage2 = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage1.add_header('Content-ID', '<image1>',)
msgRoot.attach(msgImage1)
msgImage2.add_header('Content-ID', '<image2>',)
msgRoot.attach(msgImage2)

# Send the email (this example assumes SMTP authentication is required)
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('user@gmail.com', 'password')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()

In the above code, the body is already in HTML, so reading in the MRTG generated HTML file is pretty straight forward. However, images are referenced using a Content ID. The original MRTG HTML file does not have this present.

<!-- Begin `Daily' Graph (5 Minute -->
 <div class="graph">
 <h2>`Daily' Graph (5 Minute Average)</h2>
 <img src="cnntcobd01-day.png" title="day" alt="day" />
 <table>
 <tr>
 <th></th>
 <th scope="col">Max</th>
 <th scope="col">Average</th>
 <th scope="col">Current</th>
 </tr>
 <tr class="in">
 <th scope="row">UP</th>
 <td>1959.4 kb/s (30.3%)</td>
 <td>408.7 kb/s (6.3%) </td>
 <td>278.4 kb/s (4.3%) </td>
 </tr>
 <tr class="out">
 <th scope="row">DN</th>
 <td>26.4 Mb/s (18.3%) </td>
 <td>5079.3 kb/s (3.5%) </td>
 <td>6192.8 kb/s (4.3%) </td>
 </tr>
 </table>
 </div>
<!-- End `Daily' Graph (5 Minute -->

Python can fix this!
Since we have to read the MRTG HTML from a file anyways, why not add the cid: to the image source in the HTML?

# We reference the image in the IMG SRC attribute by the ID we give it below
# Open up the existing HTML file and add the CID before sending the email
with open('%s/target.html' % basepath, 'r') as f:
 newlines = []
 for line in f.readlines():
 newlines.append(line.replace('<img src="', '<img src="cid:'))
f.close()
with open('%s/target.html' % basepath, 'w') as f:
 for line in newlines:
 f.write(line)
f.close()
f = open('%s/target.html' % basepath, 'r')
body = ''.join(f)
f.close()

I’ve replaced <img src=” with <img src=”cid:

This now creates the appropriate Content ID within the HTML message so all the images are displayed inline.

Working Code

# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.

from datetime import date, timedelta
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

# Base path for image files
basepath = '/opt/mrtg'

# Define these once; use them twice!
strFrom = 'user@gmail.com'
strTo = ['user1@youcompany.com', 'user2@yourcompany.com']
strSubject = 'Your Important Message'

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = strSubject
msgRoot['From'] = strFrom
msgRoot['To'] = &quot;, &quot;.join(strTo)
msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
# Open up the existing HTML file and add the CID before sending the email
with open('%s/target.html' % basepath, 'r') as f:
 newlines = []
 for line in f.readlines():
 newlines.append(line.replace('<img src="', '<img src="cid:'))
f.close()
with open('%s/target.html' % basepath, 'w') as f:
 for line in newlines:
 f.write(line)
f.close()
f = open('%s/target.html' % basepath, 'r')
body = ''.join(f)
f.close()
# print body
msgText = MIMEText(body, 'html')
msgAlternative.attach(msgText)
# Use the basepath directory to find the gif files
fp = open('%s/target-day.png' % basepath, 'rb')
msgImage1 = MIMEImage(fp.read())
fp.close()
fp = open('%s/target-week.png' % basepath, 'rb')
msgImage2 = MIMEImage(fp.read())
fp.close()
fp = open('%s/target-month.png' % basepath, 'rb')
msgImage3 = MIMEImage(fp.read())
fp.close()
fp = open('%s/target-year.png' % basepath, 'rb')
msgImage4 = MIMEImage(fp.read())
fp.close()
fp = open('%s/mrtg-l.png' % basepath, 'rb')
msgImage5 = MIMEImage(fp.read())
fp.close()
fp = open('%s/mrtg-m.png' % basepath, 'rb')
msgImage6 = MIMEImage(fp.read())
fp.close()
fp = open('%s/mrtg-r.png' % basepath, 'rb')
msgImage7 = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage1.add_header('Content-ID', '<target-day.png>',)
msgRoot.attach(msgImage1)
msgImage2.add_header('Content-ID', '<target-week.png>',)
msgRoot.attach(msgImage2)
msgImage3.add_header('Content-ID', '<target-month.png>',)
msgRoot.attach(msgImage3)
msgImage4.add_header('Content-ID', '<target-year.png>',)
msgRoot.attach(msgImage4)
msgImage5.add_header('Content-ID', '<mrtg-l.png>',)
msgRoot.attach(msgImage5)
msgImage6.add_header('Content-ID', '<mrtg-m.png>',)
msgRoot.attach(msgImage6)
msgImage7.add_header('Content-ID', '<mrtg-r.png>',)
msgRoot.attach(msgImage7)

# Send the email (this example assumes SMTP authentication is required)
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('user@gmail.com', 'password')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()

End Result

This is an 8 pair ADSL2+ EFM bonded group running at 144 Megbits/s downstream and 6.7 Megabits/s upstream.

MRTG GRAPH

Posted on June 29, 2013, in MRTG, Python, Scripting and tagged , . Bookmark the permalink. 2 Comments.

  1. Thanks a lot for this elborate tutorial. It’s really Wonderful.

  1. Pingback: » Python:Python: Sending Multipart html emails which contain embedded images

Leave a comment