如何使用python将.html文件作为文本插入Outlook



我想使用 python 将.html文件作为文本插入。我正在使用win32com,但问题是它正在附件中附加文件,我想将其插入主体中。

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 = "abc@email.com"
attachment1 = "D:WorkReport_autoReport.htm" 
newMail.Attachments.Add(attachment1)
newMail.display()
newMail.send()

您可能需要先将索引.html转换为字符串并与邮件连接。HTML博迪

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

如果有人在这种情况下使用 smtplib,我们需要使用以下代码片段:

  with open('file.html', 'r') as myfile2:
            data2 = myfile2.read()
        body2 = data2
        message2 = MIMEMultipart()
        message2["From"] = abc@outlook.com
        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();

最新更新