I am able to send email using outlook with below script, but if I try to send attachment it gives error.
File Name_Email.xlsx
NAME EMAIL
Roy Roy@gmail.com
Jack Jack@gmail.com
Python Script
import win32com.client as win32
import pandas as pd
email_list = pd.read_excel(r'C:\Users\roy\Name_Email.xlsx')
names = email_list['NAME']
emails = email_list['EMAIL']
for i in range(len(emails)):
name = names[i]
email = emails[i]
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = email
mail.Subject = 'Message subject'
mail.Body = 'Hello ' + name
attachment = "hosts.txt"
mail.Attachments.Add(attachment)
mail.Send()
error i am getting:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot find this file. Verify the path and file name are correct.', None, 0, -2147024894), None)
The file name is correct, and it's in same directory as the script.
Also I tried to change attachment = 'hosts.txt', but same error. Not sure what is missing.