[Tutor] Insert time into email
Bo Morris
crushed26 at gmail.com
Mon Oct 20 14:34:34 CEST 2014
hello all, hope everyone is doing well.
The below code works, however I am going back and trying to enter the time
and date and I cant quite figure out how to do this without breaking the
code.
#!/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 = "engineering at oneconnxt.com"
#strTo = "mmedley at onemediacorpinc.com"
strTo = "bo at onemediacorpinc.com"
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']
t = time.strftime("%H:%M:%S")
d = time.strftime("%d/%m/%Y")
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="1"><tr><td><img
src="cid:3102EHD-01108.png" width="400"
height="300"></img><table><tr><td>TIME<td><td>DATE</td></td></tr></table></td><td><img
src="cid:3102DHD-01109.png" width="400"
height="300"></img><table><tr><td>TIME<td><td>DATE</td></td></tr></table></td></tr><table
cellspacing="15" border="1"><tr><td><img src="cid:3102EHD-01082.png"
width="400"
height="300"></img><table><tr><td>TIME</td><td>DATE</td></tr></table></td><td><img
src="cid:3102DHD-01033.png" width="400"
height="300"></img><table><tr><td>TIME</td><td>DATE</td></tr></table></td></tr></table><table
cellspacing="15" border="1"><tr><td><img src="cid:3102EHD-01302.png"
width="400"
height="300"></img><table><tr><td>TIME</td><td>DATE</td></tr></table></td><td><img
src="cid:3102DHD-01149.png" width="400"
height="300"></img><table><tr><td>TIME</td><td>DATE</td></tr></table></td></tr><table
cellspacing="15" border="1"><tr><td><img src="cid:3102EHD-01125.png"
width="400"
height="300"></img><table><tr><td>TIME</td><td>DATE</td></tr></table></td><td><img
src="cid:3102DHD-01144.png" width="400"
height="300"></img><table><tr><td>TIME</td><td>DATE</td></tr></table></td></tr></td></tr></table><table
cellspacing="15" border="1"><tr><td><img src="cid:3102DHD-01144.png"
width="400"
height="300"></img><table><tr><td>TIME</td><td>DATE</td></tr></table></td></tr></table>',
'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"
I need to enter the time and date in the html where "TIME" and "DATE" are.
I imagine I can do this by adding "cid: t" and "cid:d" which just refers
back to t = "time.strftime("%H:%M:%S")" "d = time.strftime("%d/%m/%Y")"?
Thanks in advance for any help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141020/2599e487/attachment.html>
More information about the Tutor
mailing list