将文本添加到现有邮件正文

时间:2014-04-02 06:22:40

标签: python outlook-2010 win32com

我想使用以下python代码自动化一些报告

from win32com import client
obj = client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(0x0)
newMail.Subject = "This is the subject"
...
newMail.Body = "This is the text I want to send in the mail body"

但这样做会删除签名。以下代码

...
newMail.Body = "This is the text I want to send in the mail body" + newMail.Body

保留签名,但会破坏格式化。由于合规原因,这是不可接受的。

有没有办法在邮件正文中添加文本以规避签名格式的终止?

1 个答案:

答案 0 :(得分:1)

tmp = newMail.Body.split('<body>')
# split by a known HTML tag with only one occurrence then rejoin
newMail.Body = '<body>'.join([tmp[0],yourString + tmp[1]])