有人知道如何使用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
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")
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 样式)。
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)
如果您的签名已设置为默认值,那么您应该能够做到这一点。
>>> signature = message.body
>>> message.body = "ahoj\n" + signature
如果您的签名包含图片,您还可以使用message.HTMLbody
。
如果将其设置为默认值,则应始终在消息中显示您的签名。您可以将正文的当前内容保存到签名变量中,然后将其添加到消息的末尾。至少对我来说是有效的。