python send email from outlook

How to send email from outlook in python

In the previous article, I have explained how to read and save attachments from the outlook by using pywin32 library. In this article, I will walk through with you how to send email from outlook with the same library.


You need to install the pywin32 library in your working environment.

pip install pywin32

and import this library in your script.

import win32com.client

Let’s get started!

You will first need to initiate the outlook application by calling the below:

outlook = win32com.client.Dispatch('outlook.application')

In outlook, email, meeting invite, calendar, appointment etc. are all considered as Item object. Hence we can use the below to create an email object:

mail = outlook.CreateItem(0)

for this mail item, there are various attributes we can set, such as the below To, CC, BCC, Subject, Body, HTMLBody etc. as well as the Attachments:

mail.To = ''
mail.Subject = 'Sample Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail.CC = ''

You can add multiple attachments by calling the Attachments.Add multiple times.

Trigger to send out email from outlook

With the above attributes set, you shall be able to send out the email since all the necessary info are provided. Below line of code will trigger to send email from outlook application.


You may also wonder what if you just want to reply to a particular email instead of writing new email? In this case, you will need to find out the email message first and then use the message.Reply() or message.ReplyAll() to reply to the original message. Do check on my this article.


This is just a sample demo of how to send emails, and there are plenty of things you can do with pywin32 library, do check my other related articles, such as this.

Last but not the least, welcome to any comments or questions. Follow me on twitter for more updates.

You may also like

5 4 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Henry Roberts

  messages = messages.Restrict(“[ReceivedTime] >= ‘” + received_dt + “‘”)
SyntaxError: invalid character in identifier

I keep getting this error when I try to run the code what can I do to fx this

Ashutosh Mishra

how to send to multiple recipients


I can’t even start the program.
install is a syntax error

Wesjan Swart

Literally the first one lol

Ruth Grace

Was very helpful! Thanks. How does it determine who is sending the email? Can I change that?


Hi Ken,

I am in the same situation where I want to send the email from a different account. Is there any way to send it from the web email of a different account?


Hi, the pywin32 library only works when you configured your mail account in outlook application. To send from web mail, you may check if this post is workable for you.


Hi, I run the script, but nothing happened, it wont bring up oulook client at all


Can u just tell me what we need to put in the place of html body


Tqq can u just say me how to send zip file in attachments for excel I have used .CSV and for zip file what need to use ?


what should I write into mail.CC section and We don’t write our email address and password how the program know our email and password ?

Is the link right for outlook client ?
After I download,should everything same like example above ? (client.Dispatch part)

Xu Gao

Very helpful! And also works on my side.
1 question, how to send email to multiple receivers?



Persistent noob

Hey great article but can you tell me what’s not working in my code? I want to send some files but it’s not letting me

import win32com.client
import glob
import os


data = [file for file in glob.glob("*.txt")]

outlook = win32com.client.Dispatch('outlook.application')

mail = outlook.CreateItem(0)

mail.To = ''

mail.Subject = 'Testing my script'


mail.CC = ''


Last edited 2 years ago by Persistent noob
Persistent noob

Is there any other solution because it’s not working
I’m trying to send some data to my email address what can i do?


Great write up. Do you know if there is a way instead of attaching I can insert item as text (similar to here ? I have writeups that I would like to be included in the body when sending meeting mintues as opposed to inserting as attachments. I am not sure how to check for this capability in the pywin32 library or other Microsoft docs. Thanks for your help.


Hey Ken,
Have you found any solution for selecting label with this library?
Labels such as – Not Classified, Public, Proprietary etc
My system used to send automatically but a few months back my organisation brought an update where we also wanted to select this label.
Now a pop up opens which asks for this label to be selected when automated run is complete.
Let me know if you have a solution.


I have two different email accounts setup in my outlook. How would I select one of them to send an email from?

Bruno Silva

Hi, Ken!

First of all, thanks for the amazing work. This script, combined with some loops, saved me tons of work! Much appreciated!

One detail I would like your help, though: how to add the e-mail signature to it?


Saul Renteria

Hello, what if I already have a sign to use, how can I choose it from outlook to use it on the e-mail?


How would I set up the attachment when my file name changes daily?


How to access outlook contact folder, if i have to send email ??
can anyone guide on this.


what if I want to send an email that is currently saved as a draft?

Would love your thoughts, please comment.x