簡體   English   中英

使用 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

使用上述答案中的代碼,包含簽名的全功能電子郵件功能:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM