Python: Send Email from Outlook with Attachment

In this post you will get to know how to send emails automatically from Microsoft Outlook using Python. This automation of mail sending from Outlook can be achieved in few lines of Python code.

Below is given the step-by-step guide to automate your outlook for mail sending.

Step 1: Installing the win32com.client

Open command prompt and type in the below given pip command to install this library.

python -m pip install pywin32

Step 2: Use this Python code snippet.

import win32com.client as win32

ol = win32.Dispatch("outlook.application")

olmailitem = 0x0 # size of the new email

newmail = ol.Createltem(olmailitem)

newmail.Subiect = 'Test Mail Subject'

newmail.To = '[email protected]'

newmail.CC='[email protected]'

newmail.BCC  = '[email protected]'

newmail.Body = 'This is sample mail for testing'

attachpath =  'D:\\Test Data\\Sample.xlsx'

newmail.Attachments.Add(attachpath)

#To display the mail item before sending it

newmail.Display()

newmail.Send()

Now open your outlook and check Sent Items, you will find a mail item with subject as “Test Mail Subject” has been sent to mentioned To, CC and BCC mail id.

Leave a Reply