![](/img/trans.png)
[英]Python styled dataframe to email using win32com 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
使用上述答案中的代码,包含签名的全功能电子邮件功能:
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.GetInspector
mail.HTMLBody
现在包含您需要合并(不仅仅是连接!)与您自己的 HTML 的消息签名
更新:从Outlook, GetInspector
技巧不再有效。 现在只有MailItem.Display
将签名添加到未修改的消息中。
如果您想以编程方式插入签名, Redemption (我是它的作者)会公开RDOSignature对象,该对象实现ApplyTo
方法(它处理签名图像文件并适当地合并 HTML 样式)。
您可以在 %APPDATA%\Microsoft\Signatures 中找到存储为 HTML 文件的 Outlook 中的签名,我使用以下代码复制文件内容并将它们添加到我的电子邮件正文
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()
如果您的签名设置为默认值,您应该能够执行此操作。
>>> signature = message.body
>>> message.body = "ahoj\n" + signature
如果您的签名包含图片,您也可以使用message.HTMLbody
。
如果您将其设置为默认值,您的签名应始终出现在消息中。 您将正文的当前内容保存到签名变量,然后将其添加到消息的末尾。 至少对我有用。
我开始应用上面的好撒玛利亚人 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.