Python Forum

Full Version: sending html file in email
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am trying to email the report.html. email is coming successfully, but without any content.
Tried with standalone python script.

following is the code:
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from pathlib import Path
report_file = open(Path("report.html"))
#alert_msg = MIMEText(report_file.read(),"html", "utf-8")

# me == my email address
# you == recipient's email address

you = "[email protected]"
me = "[email protected]"
subject = 'Test Subject v5'

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = report_file.read()

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

server = smtplib.SMTP(server-ip)

server.send_message(msg)
#server.sendmail(me,you,msg.as_string())
server.quit()
contents for report.html


Output:
<html> <head> <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <!-- Latest compiled and minified CSS --> <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/slate/bootstrap.min.css" rel="stylesheet" integrity="sha384-X9JiR5BtXUXiV6R3XuMyVGefFyy+18PHpBwaMfteb/vd2RrK6Gt4KPenkQyWLxCC" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> <!-- Local resources --> <link rel="stylesheet" href=".html/report.css"> <script src=".html/report.js"></script> <!-- Reporting data --> <script src="report.js"></script> </head> <body> <div class="container-fluid" id="root"></div> </body> </html>
What happens if you only send html, as the only message part?
how to send only html?
Here's what you do currently:
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)
Don't attach part1, so the only content on the email is the html content.