Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Gmail/Gmail_Get_most_important_unseen_emails.ipynb
2973 views
Kernel: Python 3

Gmail.jpg

Gmail - Get most important unseen emails

Give Feedback | Bug report

Tags: #gmail #productivity #naas_drivers #operations #automation #analytics #plotly

Last update: 2023-07-19 (Created: 2023-07-19)

Description: This notebook retrieves all emails for a set period of time and calculates the user's reply rate to each sender. By identifying the important senders with a higher reply rate, the code helps prioritize the user's responses and ensures timely communication. The code then outputs the list of unseen emails from these important senders, providing a focused view of the most relevant and pending email conversations.

Input

Import libraries

import datetime import os try: from imapclient import IMAPClient except: !pip install imapclient --user from imapclient import IMAPClient import naas from collections import Counter import quopri import email.header

Setup Variables

Create an application password following this procedure

  • username: This variable stores the username or email address associated with the email account

  • password: This variable stores the password or authentication token required to access the email account

  • date_start: Number of days to filter your inbox, it must be negative value

  • most_common_senders: Number of most common senders you want to list as output

username = "xxxxx@xxxxx" password = naas.secret.get("GMAIL_APP_PASSWORD") date_start = -30 most_common_senders = 10

Model

Connect to email box

server = IMAPClient('imap.gmail.com') server.login(username, password) server.select_folder('INBOX') print("✅ Successfully connected to INBOX")

Get all seen emails with their flags (seen or unseen), date, and sender

today = datetime.date.today() start = today + datetime.timedelta(days=date_start) all_messages = server.search(['SINCE', start.strftime('%d-%b-%Y')]) all_metadata = server.fetch(all_messages, ['RFC822.SIZE', 'FLAGS', 'INTERNALDATE', 'ENVELOPE']) print("✅ All emails fetched:", len(all_metadata))

Get most viewed senders by counting the occurencies

senders = [] answered_senders = [] for msg_id, data in all_metadata.items(): envelope = data[b'ENVELOPE'] if envelope.from_: sender_email = envelope.from_[0].mailbox.decode() + "@" + envelope.from_[0].host.decode() senders.append(sender_email) if b'\\Answered' in data[b'FLAGS']: answered_senders.append(sender_email) sender_counts = Counter(senders) answered_sender_counts = Counter(answered_senders) rate_by_sender = {} for sender, count in sender_counts.items(): answered_count = answered_sender_counts[sender] if sender in answered_sender_counts else 0 rate = (answered_count / count) * 100 rate_by_sender[sender] = rate sorted_senders = sorted(rate_by_sender.items(), key=lambda x: x[1], reverse=True) top_senders = sorted_senders[:most_common_senders] print("✅ Top senders by answering rate:") for sender, rate in top_senders: print(f"Sender: {sender}, Answering Rate: {rate}%")

Identify the unseen emails from the top senders

unseen_emails = [] for sender, count in top_senders: unseen_messages = server.search(['UNSEEN', 'FROM', sender, 'SINCE', start.strftime('%d-%b-%Y')]) unseen_emails.extend(unseen_messages) len(unseen_emails)

Extract the date, sender, and subject from the unseen_emails list to provide data for the output

email_list = [] for msg_id in unseen_emails: email_data = server.fetch(msg_id, ['ENVELOPE'])[msg_id][b'ENVELOPE'] sender = email_data.from_[0].mailbox.decode() + "@" + email_data.from_[0].host.decode() date = email_data.date.strftime("%Y-%m-%d") subject_bytes = email_data.subject.decode() subject = email.header.decode_header(subject_bytes)[0][0] if isinstance(subject, bytes): subject = subject.decode() email_list.append((sender, date, subject))

Output

for sender, date, subject in email_list: print(f"{sender:<40} {date:<20} \"{subject}\"")