Bottom Page

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 = "receiver@example.com"
me = "sender@example.com"
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>
buran wrote Apr-03-2019, 10:36 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#2
What happens if you only send html, as the only message part?
Quote
#3
how to send only html?
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 108 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 150 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  How to Find & Count String Patterns Between two Markers in a HTML file ahmedwaqas92 3 182 Aug-19-2019, 10:12 AM
Last Post: ahmedwaqas92
  read text file and write into html with correct link jacklee26 4 239 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  Save code with output to HTML file type agohir 2 445 Mar-26-2019, 05:17 PM
Last Post: agohir
  Search for a text in a html file ebolisa 4 408 Feb-09-2019, 10:46 PM
Last Post: ebolisa
  Python win32com add image in HTML body of email Joe_Dananza 0 2,887 Sep-09-2018, 02:48 PM
Last Post: Joe_Dananza
  sending email by exchangelib Caunnabeau 0 2,216 Sep-07-2018, 05:03 AM
Last Post: Caunnabeau
  Problem in sending email using python nhoh007 1 588 Aug-25-2018, 07:20 PM
Last Post: typic
  Download entire web pages and save them as html file with urllib.request fyec 2 2,913 Jul-13-2018, 10:12 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)