使用win32com在python中为outlook电子邮件添加签名

6

有人知道如何使用win32com在电子邮件中添加电子邮件签名吗?

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'TO'
mail.Subject = 'SUBJECT'
mail.HTMLbody = 'BODY'
mail.send
5个回答

10
一个拥有签名的完全功能的电子邮件发送函数,使用上面回答中的代码:
def Emailer(message, subject, recipient):
    import win32com.client as win32   

    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.GetInspector 

    index = mail.HTMLbody.find('>', mail.HTMLbody.find('<body')) 
    mail.HTMLbody = mail.HTMLbody[:index + 1] + message + mail.HTMLbody[index + 1:] 

    mail.Display(True)
    #mail.send #uncomment if you want to send instead of displaying

那么请调用。
Emailer("Hi there, how are you?", "Subject", "david@bisnode.com")

如果您想添加附件,可以将其添加到代码中:if attachment != '': mail.Attachments.Add(attachment) - Chadee Fouad

6

Outlook 签名不通过 Outlook 对象模型公开。您能做的最好的事情是从文件系统中读取签名,并将其内容适当地添加到 HTML 正文中。请记住,必须合并两个 HTML 字符串,而不仅仅是连接它们。您还需要合并两个 HTML 文档的样式,并处理签名使用的嵌入式图像。

请注意,当显示未修改的邮件或触摸其检查器时,Outlook 会添加签名。

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'TO'
mail.Subject = 'SUBJECT'
mail.Display

mail.HTMLBody 现在包含您需要与自己的 HTML 合并 (而不仅仅是连接!) 的消息签名。

更新: 截至最新的 (2016 年夏季) Outlook, GetInspector 版本构建,技巧不再适用。现在只有 MailItem.Display 将签名添加到未修改的消息中。
如果您想以编程方式插入签名,Redemption (我是其作者) 公开了实现 ApplyTo 方法的 RDOSignature 对象 (它处理签名图像文件并适当地合并 HTML 样式)。


那个方法可以成功地将签名添加进去。希望我做得对,但是我在mail.GetInspector之后添加了以下几行代码:signiture = mail.HTMLbody and mail.HTMLbody = (r"""MESSAGE""") + signiture。这样合并两个项目是最好的方式吗?我是Python新手,所以想确保我使用了正确的方法。 - Michael David
不,您不能将两个格式良好的HTML字符串连接起来并得到一个格式良好的HTML字符串。- 读取HTMLBody属性(现在将包括签名),找到“<body”字符串的位置。然后向前移动到下一个“>”字符(这是为了确保您可以使用具有属性的body元素),然后在该“>”字符之后插入您的HTML字符串。 - Dmitry Streblechenko
1
谢谢你带我走过那段路。我现在明白了。格式看起来正确了,但如果我还做错了什么,请告诉我。我添加了一个带有我的文本的消息变量和一个索引变量:index = mail.HTMLbody.find('>', mail.HTMLbody.find('<body')) 然后修改了正文字符串为 mail.HTMLbody = mail.HTMLbody[:index + 1] + message + mail.HTMLbody[index + 1:] 现在一切看起来都正确。 - Michael David

2
你可以在%APPDATA%\Microsoft\Signatures文件夹中找到Outlook存储的签名,它是以HTML文件的形式保存的。我使用以下代码复制文件内容并将其添加到我的电子邮件正文中。
import win32com.client
import os 

     
    
signature_path = os.path.join((os.environ['USERPROFILE']),'AppData\Roaming\Microsoft\Signatures\Work_files\\') # Finds the path to Outlook signature files with signature name "Work"
html_doc = os.path.join((os.environ['USERPROFILE']),'AppData\Roaming\Microsoft\Signatures\Work.htm')     #Specifies the name of the HTML version of the stored signature
html_doc = html_doc.replace('\\\\', '\\') #Removes escape backslashes from path string


html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore') #Opens HTML file and ignores errors
signature_code = html_file.read()               #Writes contents of HTML signature file to a string
signature_code = signature_code.replace('Work_files/', signature_path)      #Replaces local directory with full directory path
html_file.close()


olMailItem = 0x0
outlook = win32com.client.Dispatch("Outlook.Application")
newMail = outlook.CreateItem(olMailItem)

newMail.CC = "my@email.com"
newMail.Subject = subject
newMail.BodyFormat = 2 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
newMail.HTMLBody = "Email Message" + signature_code
newMail.display()

看起来我需要用绝对路径替换本地路径以使用图片等签名文件。

sig_files_path = 'AppData\Roaming\Microsoft\Signatures\\' + signature_name + '_files\\'
    sig_html_path = 'AppData\Roaming\Microsoft\Signatures\\' + signature_name + '.htm'

    signature_path = os.path.join((os.environ['USERPROFILE']), sig_files_path) # Finds the path to Outlook signature files with signature name "Work"
    html_doc = os.path.join((os.environ['USERPROFILE']),sig_html_path)     #Specifies the name of the HTML version of the stored signature
    html_doc = html_doc.replace('\\\\', '\\')


    html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore') #Opens HTML file and converts to UTF-8, ignoring errors
    signature_code = html_file.read()               #Writes contents of HTML signature file to a string

    signature_code = signature_code.replace((signature_name + '_files/'), signature_path)      #Replaces local directory with full directory path
    html_file.close()

我喜欢你在这里所做的 - 我正在尝试做同样的事情,并且可以通过并成功地发送所有内容 - 但是我无法在电子邮件中呈现图片 - 只有红色的“X”和“无法下载图片”。 - Hatt
我自己遇到了这个问题,似乎需要用绝对路径替换本地路径以访问签名文件。 - linktotherescue

0

我开始应用那位好心人 linktotherescue 上面给我们发布的代码。

在研究了检查器功能后,我通过在Outlook上进行另一项签名并将当前图像更改为称为{IMAGE}的文本来实现它,然后使用“查找”搜索该文本并用原始签名中的图像替换它。

import win32com.client as win32
import os
import codecs

sig_files_path "C://Users//RenterSa//AppData//Roaming//Microsoft//Signatures//Technical Support Engineer_archivos"
sig_html_path = "C://Users//RenterSa//AppData//Roaming//Microsoft//Signatures//TSE (Python).htm"
img_path = r'C:\Users\RenterSa\AppData\Roaming\Microsoft\Signatures\Technical Support Engineer_archivos\image001.jpg'

signature_path = os.path.join((os.environ['USERPROFILE']), sig_files_path) # Finds the path to Outlook signature files with signature name "Work"
html_doc = os.path.join((os.environ['USERPROFILE']),sig_html_path)     #Specifies the name of the HTML version of the stored signature
html_doc = html_doc.replace('\\\\', '\\')

html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore') #Opens HTML file and converts to UTF-8, ignoring errors
signature_code = html_file.read()               #Writes contents of HTML signature file to a string

signature_code = signature_code.replace((sig_html_path + sig_files_path), 
signature_path)      #Replaces local directory with full directory path
html_file.close()

olMailItem = 0x0
outlook = win32.Dispatch("Outlook.Application")
newMail = outlook.CreateItem(olMailItem)

newMail.CC = "my@email.com"
newMail.Subject = "subject"
newMail.Importance = 2
newMail.BodyFormat = 3  # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
newMail.HTMLBody = "Email Message" + signature_code
inspector = newMail.GetInspector
newMail.display()
doc = inspector.WordEditor
selection = doc.Content
selection.Find.Text = r"{IMAGE}"
selection.Find.Execute()
selection.Text = ""
img = selection.InlineShapes.AddPicture(img_path, 0 , 1)

0

如果您的签名已设置为默认值,那么您应该能够做到这一点。

>>> signature = message.body
>>> message.body = "ahoj\n" + signature

如果您的签名包含图片,您还可以使用message.HTMLbody

如果将其设置为默认值,则应始终在消息中显示您的签名。您可以将正文的当前内容保存到签名变量中,然后将其添加到消息的末尾。至少对我来说是有效的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接