[Tutor] Insert time into email

Chris “Kwpolska” Warrick kwpolska at gmail.com
Mon Oct 20 18:02:14 CEST 2014


On Mon, Oct 20, 2014 at 2:34 PM, Bo Morris <crushed26 at gmail.com> wrote:
> 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"

PS. You may want to use a real e-mail address here.  Or, at the very
least, something that looks like one.

> #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")"?

No, not really.  cid: is for images.  You want to insert actual text.
Use string formatting.

'…snip…<tr><td>{time}</td><td>{date}</tr>…snip…'.format(time=t, date=d)

This is assuming every date is the same.  If it isn’t, you’d have to
use different identifiers and values.

-- 
Chris “Kwpolska” Warrick <http://chriswarrick.com/>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense


More information about the Tutor mailing list