How to SEARCH and ORGANIZE your mailbox with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this video i'm going to show you how to search and organize your mail in outlook using python also i'm going to show you a method for recursively searching and collecting every message in your account folder that meets your search criteria this is a method you can customize for whatever search operation you want to perform on your own this video is part of a series on how to get the most out of outlook using python i'm going to demonstrate this lesson using a jupiter notebook but you can use any ide or code editor you wish so without further ado let's get started first go ahead and import the win32com library and start up an instance of outlook next since we're going to be working with folders we need to create the namespace object since i don't have a lot of mail in my testing account i'm going to go ahead and show you this with my gmail account which i've connected to my outlook application this makes it possible to access and manipulate my gmail with the win32com library through the namespace object now typically i would use the getdefaultfolder method to access folders but since my gmail account isn't the default profile in outlook i need to explicitly ask for it so all i need to do to access my gmail account folder is to index it with the account name as the key and to get the inbox it's the same process i can confirm that i have the correct folders by printing the folder name and the parent folder name we're first going to do a simple search there's actually a search mechanism built into the outlook api you're free to look it up however after about 15 minutes i realized it was going to be much simpler and more useful to use a pure python approach using the objects that we've already learned about so far in this series as you know each folder contains an items collection and that collection contains outlook objects because i'm in a mail folder the items collection contains email messages and you can access any of the message properties we're going to use this knowledge to search those property values for specific terms the easiest way to get what you want is to use the string methods starts with ends with and then the equals and in operators let's see how this works in real life i'm going to use a list comprehension to collect every message in my inbox that has a sender email address that ends with youtube.com [Music] if i print these messages i can see that they are indeed youtube emails what i'm going to do next is create a youtube email folder in my inbox with the folders.add method saving a reference to it with the yt folder variable then i'm going to move all of these youtube messages to that folder using the move method passing in the target folder object as the argument because i used sender email address endswithyoutube.com i'm moving any email address with that ending so for example if you wanted to collect all emails you received from different people but who all worked at the same company you could collect those emails in this way as long as they all end with the same term the startswithstring method works just the same but in reverse and the equals operator would match the term exactly so for example if i had a specific email address i wanted to filter i could use the equals operator let's do one more simple search technique before we move on to something a little more advanced let's collect all messages in my inbox where the term deal is in the body of the email for this we're going to use the body property and we're going to use the string method lower so that i don't have to worry about capitalization okay let's go ahead and print all of these sender email addresses [Music] yeah it makes sense that i would be catching these messages they're all people trying to sell me stuff i don't need and if i print the subject line let's create a folder in the inbox called junk stuff and we'll move all of these messages into that folder are you enjoying this video so far if so make sure you hit that like button and subscribe now for something a bit more advanced up to now we've been performing searches in a single folder but as you can see here i have many folders in my gmail account and the inbox is only one of them what if i wanted to search every folder for messages containing a specific term or phrase in order to do this i need to use recursion a recursive function is one that calls itself if that sounds strange it is but it's also awesome as you'll see shortly and for some reason recursion makes me think of russian dolls so let's create a function called mailbody search and this function is going to accept two arguments the search term and the folder object now the logic of this function is as follows i'm going to collect every message in the folder that meets the criteria i specify i'm also going to return the parent name so that you can see what subfolder it's coming from now this isn't required i just want you to be able to see that we are indeed searching off folders i'm then going to check to see if there are any subfolders if not i'm done this is the criteria that ends this infinite loop and this is also called the base case and every recursion formula needs a base case however if there are subfolders i'm going to call this function on each of those subfolders adding the returned results to my relevant message list and repeating this russian doll exercise until at some point i reach that base case a folder has no subfolders i highly recommend doing a search on youtube for recursion if you want to learn more about this topic it's very useful and once you get the hang of it you can do some pretty cool things finally my function will return the collected search results once i reach that base case and my russian doll function rolls up the results okay let's try this out i'm going to search for the term python in my gmail account and since i want to include all folders i'm going to start with the account level folder i'm going to go ahead and save this to a csv file because i have so many emails it's going to be much easier for you to see what i have here i'm going to output the parent folder and then the subject line [Music] you can see that i've hit many folders here so i know that it worked now when you write this formula you don't need to include the parent name and the results i just did it for testing however if i wanted to move all of these emails or delete them i now have a list of meal item objects that meet my search criteria and i can do whatever i want with them at this point you can see that with a little bit of code you can quickly and easily automate and organize your mailbox with python and that's all folks if you want to see me make a video on some topic related to outlook and python leave your ideas and thoughts in the comments section below i'd love to hear what you have in mind make sure you hit that like button and subscribe for more content like this see you in the next video [Music]
Info
Channel: Izzy Analytics
Views: 23,663
Rating: undefined out of 5
Keywords: python outlook automation, python outlook api, python outlook authentication, python outlook connection, python outlook email, python outlook email send, python outlook library, python outlook mail api, python outlook, outlook python, python outlook from, python email automation outlook, outlook python API, automate outlook email using Python, python email
Id: iP1Ko9sFkzs
Channel Id: undefined
Length: 8min 20sec (500 seconds)
Published: Mon Aug 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.