Reading Gmail with Python - Part TWO
This is a follow-up to my previous blog from my automating reading Gmail messages with Python. If you haven’t checked it out yet, read that blog post before this one.
Another cool thing you can do with Python is reading emails using the Python. In this blog post, I’ll be using a library called “IMAP tools”. IMAP refers to an Internet Message Access Protocol. Unfortunately, Yagmail can only send emails so we’ll have to install a new library in this blog post. If you’re curious, you can read the documentation for that library here.
INSTALLING IMAP TOOLS
Similarly to last time, we’re just going to type in “pip install imap-tools”. Alternatively, you can use an IDE like VSCode and use the terminal there!
PYTHON SCRIPT
from imap_tools import MailBox, AND, MailMessageFlags
PERSONAL_EMAIL = 'yourpersonalemailhere@gmail.com'
PASSWORD = '16passhere'
def check_message(msg):
filename = "".join(char if char.isalnum() else "_" for char in msg.subject) + ".txt"
with open(filename, "w") as file:
file.write(msg.text)
with MailBox('imap.gmail.com').login(PERSONAL_EMAIL, PASSWORD) as mailbox:
for msg in mailbox.fetch():
if MailMessageFlags.SEEN not in msg.flags:
check_message(msg)
I’ve wrote the code entirely myself using the iMap Tools library. The code I’ve wrote checks if a message is unseen. If it is a new text file containing the emails subject and inner text will be created.
Before Running the Program
Firstly, create a new folder and store the Python file within this folder. This will be where all the unseen emails will be stored in .txt files.
The Variables and Importing the Library
from imap_tools import MailBox, AND, MailMessageFlags
PERSONAL_EMAIL = 'yourpersonalemailhere@gmail.com'
PASSWORD = '16passhere'
Here, I’m importing the necessarry things from the library and am declaring the constants that we’ll be using. In the PERSONAL_EMAIL variable, store your gmail email and in the password variable, enter your 16 character password that was received in part one.
2. The Library Using Gmail
with MailBox('imap.gmail.com').login(PERSONAL_EMAIL, PASSWORD) as mailbox:
for msg in mailbox.fetch():
if MailMessageFlags.SEEN not in msg.flags:
check_message(msg)
Firstly, we’ll log in with the supplied username and password in the mailbox. Next, we use a for loop to get each message in the mailbox. For each message, if it is not seen, we’ll call a function to store the message subject and text to a .txt file.
3. The Check Message Function
def check_message(msg):
filename = "".join(char if char.isalnum() else "_" for char in msg.subject) + ".txt"
with open(filename, "w") as file:
file.write(msg.text)
This function will check if the file name is appropriate and doesn’t contain any character that Windows can’t recognise in the subject line e.g “?”. This will then store the subject name as the .txt file. The new file name will then be written to a new file and the message text will be stored in the respective text file. This rinses and repeats for every message.
Quite a simple program but it works rather well if you want all your emails stored somewhere!
Author: Sophie Dillon