[Tutor] Insert time into email...final code

Bo Morris crushed26 at gmail.com
Wed Oct 22 16:58:47 CEST 2014


Just in case anyone else can benefit from this, here is my working code up
to this point

#!/usr/bin/python

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

strFrom = "HourlyReport.com"

strTo = "myEmail at server.com"

t = time.strftime("%H:%M:%S  %m/%d/%y")

l = ['3102EHD-01108.png', '3102DHD-01109.png','3102EHD-01082.png',
'3102DHD-01033.png', '3102EHD-01302.png', '3102DHD-01149.png',
'3102EHD-01125.png', '3102DHD-01144.png', '3102EHD-01105.png']

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Test Hourly Report'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

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

msgText = MIMEText('<table cellspacing="15" border="0"><tr><td
align="center"><img
src="cid:3102EHD-01108.png"><br/>3102EHD-01108<br/>{time}</td><td
align="center"><img
src="cid:3102DHD-01109.png"><br/>3102DHD-01109<br/>{time}</td></tr><tr><td
align="center"><img
src="cid:3102EHD-01082.png"><br/>3102EHD-01082<br/>{time}</td><td
align="center"><img
src="cid:3102DHD-01033.png"><br/>3102DHD-01033<br/>{time}</td></tr><tr><td
align="center"><img
src="cid:3102EHD-01302.png"><br/>3102EHD-01302<br/>{time}</td><td
align="center"><img
src="cid:3102DHD-01149.png"><br/>3102DHD-01149<br/>{time}</td></tr><tr><td
align="center"><img
src="cid:3102EHD-01125.png"><br/>3102EHD-01125<br/>{time}</td><td
align="center"><img
src="cid:3102DHD-01144.png"><br/>3102DHD-01144<br/>{time}</td></tr><tr><td
align="center"><img
src="cid:3102EHD-01105.png"><br/>3102EHD-01105<br/>{time}</td></tr></table>'.format(time=t),
'html')

msgAlternative.attach(msgText)


for image in l:
    with open(image, 'rb') as fh:
        msgImage = MIMEImage(fh.read())
        msgImage.add_header('Content-ID', '<{0}>'.format(image))
        msgRoot.attach(msgImage)


try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(strFrom, strTo, msgRoot.as_string())
   print "Successfully sent email"
except smtplib.SMTPException:
   print "Error: unable to send email"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141022/2ea84627/attachment.html>


More information about the Tutor mailing list