使用 win32com 使用 python 向 outlook 电子邮件添加签名

Add signature to outlook email with python using win32com

有谁知道如何使用 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

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.GetInspector 

mail.HTMLBody 现在包含您需要将其与您自己的 HTML

合并(不仅仅是串联!)的消息签名

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

包含签名的全功能电子邮件功能,使用上述答案中的代码:

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")

如果您的签名设置为默认签名,您应该能够执行此操作。

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

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

如果您将其设置为默认签名,您的签名应该始终出现在邮件中。您会将正文的当前内容保存到签名变量,然后将其添加到消息的末尾。至少对我有用。

您可以在 Outlook 中找到签名,该签名存储为 %APPDATA%\Microsoft\Signatures 中的 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()

我开始应用好心人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)