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

Gmail.jpg

Gmail - Get emails stats by sender

Give Feedback | Bug report

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

Last update: 2023-05-12 (Created: 2023-05-12)

Description: This notebook allows users to get stats from their emailbox by sender.

Input

Import libraries

import naas from naas_drivers import email import pandas as pd import numpy as np import plotly.express as px

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

  • smtp_server: This variable represents the SMTP server address used for sending emails.

  • box: This variable stores the name or identifier of the mailbox or folder within the email account that will be accessed.

username = "xxxxx@xxxxx" password = naas.secret.get("GMAIL_APP_PASSWORD") smtp_server = "imap.gmail.com" box = "INBOX"

Model

Connect to email box

emails = email.connect(username, password, username, smtp_server)

Get email list

df_emails = emails.get() print(f"✅ Emails fetched:", len(df_emails)) df_emails.head(1)

Creating dataframe and inserting values

sender_name = [] sender_email = [] for df in df_emails["from"]: sender_name.append(df["name"]) sender_email.append(df["email"]) result = pd.DataFrame(columns=["SENDER_NAME", "SENDER_EMAIL", "COUNT", "PERCENTAGE"]) name_unique = np.unique(sender_name) email_unique = np.unique(sender_email) total_email = len(df_emails) c = 0 for i in np.unique(sender_name): new_row = { "SENDER_NAME": i, "SENDER_EMAIL": sender_email[c], "COUNT": sender_name.count(i), "PERCENTAGE": round(((sender_name.count(i)) / total_email) * 100), } result = result.append(new_row, ignore_index=True) c += 1 result

Output

Email graph plot

fig = px.bar(x=result["COUNT"], y=result["SENDER_NAME"], orientation="h") fig.show()