通过Python发送Outlook模板

问题描述

我有以下代码将电子邮件发送给指定的收件人,但是我要发送的电子邮件是标准的,长格式的。是否可以将原始电子邮件另存为模板,然后仅在我的代码中引用而不是在此处创建电子邮件

olFormatHTML = 2
olFormatPlain = 1
olFormatRichText = 3
olFormatUnspecified = 0
olMailItem = 0x0
obj = win32com.client.dispatch("outlook.application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "The Gap: " + str(res)
newMail.BodyFormat = olFormatHTML    #or olFormatRichText or olFormatPlain
newMail.HTMLBody = xxxxxxxxxx
newMail.To = xxxxxxxxxxxxxx 
newMail.Send()

解决方法

您可以打开模板电子邮件以获取html

outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")  
template_mail = outlook.OpenSharedItem(<template email path>)
template_html = template_mail.HTMLBody

然后将其放入newMail

newMail.HTMLBody = template_html