create Email with multiple HTML blocks embedded

kevind0718 at gmail.com kevind0718 at gmail.com
Tue Jan 5 10:27:03 EST 2016


The below script will send an email with one HTML file embedded and the second attached.  Not really what I need.  I need a Python script to create an email that contains multiple blocks of HTML in the body of the email.

There is a process that will create at least one HTML file but very often two. A summary and an exception web pages. These need to be emailed to a user group.

The code below displays one file in the email, but the other shows up as an attachment. If I reverse the body.attach statements then the files switch. I need both to appear in the body of the email.

I messed around with the boundary attribute, but could not get that to work.

Your kind assistance is requested.

Best Regards

KD


import smtplib
from pprint import pprint
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart()
msg['Subject'] = 'email from Python with HTML content '
msg['From'] = 'kduf at xxx.com'
msg['To'] = 'kduf at xxx.com'

text = "\nBelow I hope will be the Summary Table in HTML\n\n"
body = MIMEMultipart('multipart')

with open("H:\\dev\\testHTML\\Exceptions_SheetDec30d.htm", "r") as fE:
    htmlE = fE.read().replace('\n', '')


with open("H:\\dev\\testHTML\\Summary_SheetDec30d.htm", "r") as f:
    html = f.read().replace('\n', '')

body.attach(MIMEText(html, 'html'))
body.attach(MIMEText(htmlE, 'html'))

msg.attach(body)

s = smtplib.SMTP('smtp.aigfpc.com')  # ('localhost')
s.sendmail('kduf at xxx.com', ['kduf at xxx.com'], msg.as_string())
s.quit()




More information about the Python-list mailing list