简体   繁体   中英

Python-based automation of Outlook using data from an Excel file for the mail body and adding a signature

I'm trying to write a Python script code wherein I'll send email notifications to my team members on a daily basis.

There are two excel sheets, let's say abc.xlsx and def.xlsx .

I already have a script that updates these files and saves them. (These files abc and def are deleted and recreated with the same name but with updated information.)

Now my goal is to attach the file abc as an attachment in the mail and add the contents of def.xlsx in the email body .

I'm trying to achieve this:

Hello All,
Please find the pending lists here as follows:

///The info from def.xlsx sheet comes here///

Thanks and regards!

/// my outlook signature///

Here is my code:

import win32com.client as win32
import pandas as pd

# reading a file, which needs to be on mail body
df1 = pd.read_excel('def.xlsx')

html_table = df1.to_html(index=False)

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = 'mail@me.com'
mail.CC = 'mail@me.com'
mail.Subject = 'Test mail'

# path to signature should be User\AppData\Roaming\Microsoft\Signatures\signature.htm
pathToIMage = r'path_to_my_signature'
attachment = mail.Attachments.Add(pathToIMage)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")

#  modify the mail body as per need
mail.Attachments.Add(Source="C:\..abc.xlsx")
body = "<p>Hi All, Please find the updates pending updates below:" + html_table + " <br>Thanks and regards <p> <figure><img src=""cid:MyId1""</figure>"
mail.HTMLBody = (body)
mail.Send()

Example: This type of output I'm expecting

Challenges:

  1. My signature will be a corrupted image with a "x" in it in the test email.
  2. My Excel sheet, which has to be on the body, won't have the same format.

I've copied all the codes from Stack overflow only. I did some of my research, but I'm not getting the expected output.

First, you may try setting the BodyFormat property before setting up the HTMLBody property.

Second, to get the signature added to the message body you need to call the Display method before setting up the HTMLBody property.

Third, the <figure> element is not supported in Outlook because Word is used as an email editor and applies its own business rules to message bodies.

Fourth, the HTMLBody property returns or sets a string which represents the message body, it is expected to get or set a full-fledged well-formed HTML document. Try to set up a well-formed HTML document and then set up a property.

I modified it. I'm still working on Challenge 2. I'll just go through the documentation that has been recommended and will share my final script.

import win32com.client as win32
import pandas as pd
import os
import codecs

df1 = pd.read_excel('mail_body.xlsx')
html_table = df1.to_html(index=False)

# below is the coding logic for signature
sig_files_path = 'AppData\Roaming\Microsoft\Signatures\\' + 'signature_file_name' + '_files\\'
sig_html_path = 'AppData\Roaming\Microsoft\Signatures\\' + 'signature_file_name' + '.htm'

signature_path = os.path.join((os.environ['USERPROFILE']), sig_files_path)
html_doc = os.path.join((os.environ['USERPROFILE']), sig_html_path)
html_doc = html_doc.replace('\\\\', '\\')

html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore')
signature_code = html_file.read()

signature_code = signature_code.replace(('signature_file_name' + '_files/'), signature_path)
html_file.close()

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = 'mail@me.com'
mail.CC = 'mail@me.com'
mail.Subject = 'TEST EMAIL'
mail.Attachments.Add(Source=r"C:\..abc.xlsx")

#  modify the mail body as per need
mail.BodyFormat = 2
body = "<p>Hi All, Please find the updates pending updates below:" + html_table + " <br>Thanks and regards <br><br>"
mail.Display()
mail.HTMLBody = body + signature_code
mail.Send()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM