Welcome folks today in this example we will be fetching recently sent
emails in gmail
using python and imaplib library. All the full source code of the application is given below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install imaplib
Now you need to enable some settings
inside your gmail account to enable imap
protocol to fetch recently sent emails
Just grant the imap
access inside your gmail account as shown above in the picture
After this make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# import the modules import imaplib import email from email.header import decode_header import webbrowser import os # establish conneection with Gmail server ="imap.gmail.com" imap = imaplib.IMAP4_SSL(server) # intantiate the username and the passwprd username ="username@gmail.com" password ="********" # login into the gmail account imap.login(username, password) # select the e-mails res, messages = imap.select('"[Gmail]/Sent Mail"') # calculates the total number of sent messages messages = int(messages[0]) # determine the number of e-mails to be fetched n = 3 # iterating over the e-mails for i in range(messages, messages - n, -1): res, msg = imap.fetch(str(i), "(RFC822)") for response in msg: if isinstance(response, tuple): msg = email.message_from_bytes(response[1]) # getting the sender's mail id From = msg["From"] # getting the subject of the sent mail subject = msg["Subject"] # printing the details print("From : ", From) print("subject : ", subject) |
Here you need to replace the username
and password
of your gmail
account and then run the python
script by typing the below command
python app.py
Now after running this script it will list all your recent sent
emails of your gmail account