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.

Prerequisite:

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 = 'contact@company.com'
mail.Subject = 'Sample Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail.Attachments.Add('c:\\sample.xlsx')
mail.Attachments.Add('c:\\sample2.xlsx')
mail.CC = 'somebody@company.com'

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.

mail.Send()

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.

Conclusion:

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 3 votes
Article Rating
Subscribe
Notify of
guest
40 Comments
Oldest
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

Wes

Help.
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?

Pythoniast

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?

ken

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.

tom

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

Ramyasree

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

Ramyasree

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 ?

Hinami

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 ?

Hinami

https://www.microsoft.com/en-us/download/details.aspx?id=56972
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?

poop

cool

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

os.chdir('C:\\Users\\SEBAA\\desktop')

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

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

mail = outlook.CreateItem(0)

mail.To = 'bobmarley2021@outlook.fr'

mail.Subject = 'Testing my script'

mail.Attachments.Add(data)

mail.CC = 'fox@outlook.fr'

mail.Send()
print("sent")

Last edited 1 year 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?

Aravind

Great write up. Do you know if there is a way instead of attaching I can insert item as text (similar to here https://www.solveyourtech.com/how-to-insert-as-text-in-microsoft-outlook-for-office-365/) ? 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.

Abhijay

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.

Guilherme

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?

Thanks!

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?

Logan

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

Shreyash

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

Tincho

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

40
0
Would love your thoughts, please comment.x
()
x