简体   繁体   中英

Python win32com outlook attach file with “insert as text” method

I am trying to send a HTML webpage via outlook with python win32com. However, I don't know how to config the add attachment call to do "insert as text" method.

Does anyone know how to do that?

import win32com.client
from win32com.client import Dispatch, constants
const=win32com.client.constants

olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "I AM SUBJECT!!"
newMail.Body = "I AM IN THE BODY\nSO AM I!!!"
newMail.To = "abc@abc.com"
attachment1 = "x:\\report.htm"

newMail.Attachments.Add(Source=attachment1)
newMail.display()
newMail.Send()

thank you very much.

you can use this

import win32com.client
from win32com.client import Dispatch, constants
const=win32com.client.constants

olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "I AM SUBJECT!!"

newMail.To = "abc@abc.com"
attachment1 = "x:\\report.htm"

with open(attachment1 , 'r') as myfile:
   data=myfile.read()

newMail.Body = "Please Find the Report here " + data

newMail.HTMLBody = "I AM IN THE BODY\nSO AM I!!!" + data
newMail.Attachments.Add(Source=attachment1)
newMail.display()
newMail.Send()

you may need to convert the html file to string first and concatenate with mail.body

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM