Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sending html file in email
#1
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>
Reply
#2
What happens if you only send html, as the only message part?
Reply
#3
how to send only html?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending email using own smtp server robertkwild 11 11,150 Jul-05-2024, 12:59 AM
Last Post: AdamHensley
  Need to replace a string with a file (HTML file) tester_V 1 2,944 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 2,879 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  is it possible to copy image from email and place into excel file? cubangt 3 3,409 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Sending Whatsapp Message in the text file ebincharles869 9 8,045 Jun-21-2022, 04:26 PM
Last Post: snippsat
  HTML file crashes program mikefirth 12 7,662 Dec-31-2021, 03:57 AM
Last Post: Pedroski55
  reading html and edit chekcbox to html jacklee26 5 5,602 Jul-01-2021, 10:31 AM
Last Post: snippsat
  code for CSV file to html file without pandas jony057 1 4,635 Apr-24-2021, 09:41 PM
Last Post: snippsat
  how to automate sending bulk emails from an excel file or a calc-file apollo 0 5,872 Dec-10-2020, 08:43 PM
Last Post: apollo
  Sending Out Email via Python JoeDainton123 1 7,905 Aug-31-2020, 12:54 AM
Last Post: nilamo

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.