5

I have been using Jupyter to make html reports, and I would like to be able to email these reports using smtplib. I have been able to successfully send the emails but have not been able to get the html report to either attach to or embed in the email.

The code I have been using looks like this...

fromaddr = "[email protected]"
toaddr = "[email protected]"

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Automatic Weekly Report"

html = open("WeeklyReport.html")
part2 = MIMEText(html.read(), 'text/html')
msg.attach(part2)

server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login("[email protected]", "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

I think the issue is mostly around the middle bit which I took and modified from https://docs.python.org/3.4/library/email-examples.html

html = open("WeeklyPnlReport.html")
part2 = MIMEText(html.read(), 'text/html')
msg.attach(part2)

When I run this code I get an email, which I open in Gmail, that is blank and has an attachment called 'noname'. Previewing it doesn't work and downloading it leads to my pc not knowing what file to open it with.

If I change the middle bit to:

part2 = MIMEText(html.read(), 'html')

I receive an email that says "message clipped", then when I click on "view entire message" a new tab pops open containing the html text of the report.

If I run with:

part2 = MIMEText(html, 'html')

I get an error " '_io.TextIOWrapper' object has no attribute 'encode' ".

I'm currently quite lost about what to do. I essentially just want the html report to be either attached to my email or be inside the email. I've looked at the documentation of MIMEText but its pretty sparse and quite a bi over my head. I'd love to know more about how it operates, but more specifically how to get the html file to either embed in the email or be attached to it.

Thanks in advance.

1 Answer 1

10

Don't specify the major content type, only specify the minor type: Not text/html, but html:

part2 = MIMEText(html.read(), 'html')  # Note: no "text/"

You don't need a multipart message unless you are sending multiple parts.

If you do use multipart, specify the minor content type. The default multipart type is multipart/mixed. You might want multipart/alternative:

msg = MIMEMultipart('alternative')

This might work for you:

from email.mime.text import MIMEText
import smtplib


fromaddr = "[email protected]"
toaddr = "[email protected]"


html = open("WeeklyReport.html")
msg = MIMEText(html.read(), 'html')
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Automatic Weekly Report"

debug = False
if debug:
    print(msg.as_string())
else:
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.starttls()
    server.login("[email protected]", "password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Rob, thank you for your answer and sorry about the late reply. It really cleared up how the whole smtplib works. It seems to be an issue with either my computer or the way my gmail is set up because I could get it to work on a friends computer. I'm still trying to figure out a solution to how to get it working on my pc and will post once I've figured it out!
hi @StuartRobertson , is there any limit on the size of html file , because i was not able to send large html files. But i was able to send smaller ones

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.