1

I want to insert a .html file as text by using python. I am using win32com but problem is that it is attaching the file in attachment i want to insert it in main body.

import win32com.client
from conf import *
const=win32com.client.constants
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "ST_Report_20" + time.strftime("%y%m%d")
newMail.Body = "Please Find the Report here " + path + "\index.html"

newMail.To = "[email protected]"
attachment1 = "D:\Work\Report_auto\Report.htm" 

newMail.Attachments.Add(attachment1)
newMail.display()

newMail.send()
2
  • What if you try to change the Body Format? newMail.BodyFormat newMail.HTMLBody = mail_body Commented May 25, 2016 at 10:01
  • @giosans What i give in newMail.HTMLBody = ----- shall I gave the path of HTML file or the code written in it ? Commented May 25, 2016 at 10:22

2 Answers 2

4

you may need to convert the index.html to string first and concatenate with mail.HTMLBody

.....
with open('index.html', 'r') as myfile:
    data=myfile.read()
newMail.HTMLBody = "Please Find the Report here " + data

.....
Sign up to request clarification or add additional context in comments.

3 Comments

let me know if this helped you :)
Hi Arun, This will help me there is one thing to edit I have to use newMail.HTMLBody Thanks :)
Could someone please help in case I am using smtplib to send the mail using python. And I want to put the content of the HTML file directly in the body of the mail not as an attachment? Thanks.
0

If someone is using smtplib in that case, we need to use the below snippet:

  with open('file.html', 'r') as myfile2:
            data2 = myfile2.read()

        body2 = data2

        message2 = MIMEMultipart()
        message2["From"] = [email protected]
        message2["To"] = ", ".join(recipients)
        message2["Subject"] = "Your subject"

        message2.attach(MIMEText(body2, "html"))
        #message2.set_payload(body2) ## it is for text
        session2 = self.create_session(config_details)
        session2.sendmail(config_details[0], self.recipients, message2.as_string())
        session2.quit();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.