【问题标题】:Python - Send HTML-formatted email via Outlook 2007/2010 and win32comPython - 通过 Outlook 2007/2010 和 win32com 发送 HTML 格式的电子邮件
【发布时间】:2014-08-30 07:00:50
【问题描述】:

有没有办法使用 Python 的 win32com.client(使用 Outlook 2007/2010)发送 HTML 格式的电子邮件。我现在使用的格式是这样的:

import win32com.client
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "the subject"
newMail.Body = "body text"
newMail.To = "recipient@example.com"
attachment1 = "c:\\mypic.jpg"
newMail.Attachments.Add(attachment1)
newMail.Send()

这将使用 Outlook 向指定的收件人发送一封由当前经过身份验证的用户发送的电子邮件,其中包含主题、内容和附加图片。

我希望能够发送内嵌图像,这可以使用“嵌入”附件来实现,或者只是使用 HTML 链接到图像,或者使用 HTML 和 Base64 编码图像嵌入图像。

HTML 是我的首选方法,但我添加到正文的任何​​ HTML 都被格式化并编码为纯文本(例如,< 变为 <)。有没有办法告诉 Outlook 正文内容是 HTML 并且应该这样解析?

【问题讨论】:

    标签: python html email outlook


    【解决方案1】:

    这是把body做成html格式的方法

    newMail.HTMLBody  = htmltext
    

    【讨论】: