Hey guys,
In this video, I will show how to use Python to download messages & attachments from Outlook. So, when I execute my script, all the messages,
including the attachments, are downloaded and saved in a separate folder. The cool thing is that this also works with
any email account you might have connected to Outlook. So, in my case, I have linked my Gmail account. And now I can also retrieve all the messages
from there. However, there is one catch. This only works on Windows, as we are going
to use the win32com library. Ok, and with that said, let us get started. First things first, we need to install the
module to interact with the outlook application. Therefore open up your command prompt or terminal
and type "pip install pywin32". Once we have gotten that out of the way, I
will open up a blank python file. As we are going to also deal with different
file paths, I will import Path from pathlib. Pathlib is a standard python module, so no
additional installation is required. Then I will import 'win32com.client'. Before interacting with the outlook application,
I will create an output folder for the emails & attachments. In my case, I would like to make this output
folder in the same working directory as my current script. To create the folder, we can use 'mkdir' from
Pathlib. By settings parents to True, pathlib will
create any missing parents directories. And in case the output folder already exists,
pathlib will raise an error, which we can ignore by setting exsit_ok to true. With that in place, let us connect to Outlook. We can do this by typing 'win32com.client.dispatch'
followed the application name. So, in our case, "Outlook. Application". From this application, we want to get the
namespace mapi. Mapi stands for "Messaging Application Programming
Interface". So basically, the Microsoft Outlook messaging
API. With that interface, we could, for example,
connect to our inbox. Now, Outlook comes already with a couple of
default folders. A number represents those folders. The inbox, for instance, has the number 6. In the official Microsoft documentation for
VBA, you can also check the numbers for the other folders. So, in case you wanted to access the 'SentMail'
folder, you would write five instead of six. For convenience, I will leave the link to
this website and some of the folder numbers in the final script as a comment. Now that we know in which folder we are working,
we can get all the messages by typing' inbox.Items'. As the last step, we just need to iterate
over all messages. We can extract further information from every
message, like the subject, body, or attachments. Just be careful with the upper and lower case
for the different properties. After retrieving the different information,
I will create a separate folder for each message. We will use this folder to save the body of
the email and the attachments in there. I will use pathlib to create a new folder
within the output directory. I will use the subject of the email for the
folder name. Then I can use 'mkdir' as seen before. Pathlib makes it also super easy to create
and write text files. For this example, I will write the email body
to a text file. So, within our new target folder, I will call
the text file 'EMAIL_BODY'. I can now take this object and write the email
body to this new text file. And with that, we are almost done. As the final step, I will iterate over the
attachments and save each attachment to our target folder using SaveAsFile. The filename will be the attachment itself. Ok, guys, and that is all there is to it. Let me zoom out a bit to see the entire script,
and then I will execute it. As a result, we will now have our new output
folder. And within this folder, we can see the different
subfolders—one folder per message. Let me also validate this by placing my Outlook
inbox next to the folders. So, for example, I have here a message with
the subject 'meeting notes'. In my meeting notes folder, I can now see
the content of the email in the text file as well as the attachment. In another email, I know that I have received
multiple attachments. So let me also navigate to that folder. And we can see that Python has saved all attachments
into that folder. But there is one more thing I want to show
you. As you might know, you can also connect different
email accounts to Outlook. In my case, I have connected my Gmail account. Every time I get a new email, it will go straight
to my inbox. Now let us see how we can connect this inbox,
as this is not part of the default Outlook folders, like the ones up here. So, let me delete the output directory and
switch back to our python script. Instead of connecting to the default inbox,
we can also be more specific. The folder name is called "codingisfun.testuser@gmail.com". Within that folder, I can connect to the subfolder
"Inbox". Ok, that is all we need to change. So, let me rerun the script. And as before, we have got now our emails
and attachments saved in the output folder. I hope you found this tutorial helpful. Let me know in the comments if you have any
questions or further automation ideas. Thanks for watching and you in the next video.