如何使用 python 在 outlook 中将 .html 文件作为文本插入

How to insert .html file in outlook as text by using python

我想使用 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:\Work\Report_auto\Report.htm" 

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

newMail.send()

您可能需要先将 index.html 转换为字符串,然后与 mail.HTMLBody

连接
.....
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();