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

Gmail.jpg

Gmail - Get emails by date

Give Feedback | Bug report

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

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

Description: This notebook provides an example of how to retrieve emails based on a specified date or filter them by 'before' or 'after' a given date. It demonstrates the process of fetching emails using date-based criteria for more targeted email retrieval.

Input

Import libraries

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

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@xxxx" password = naas.secret.get("GMAIL_APP_PASSWORD") smtp_server = "imap.gmail.com" box = "INBOX" date = date(2023, 6, 5) condition = "on" # "on", "before", "after or on"

Model

Connect to email box

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

Get email list on a certain date

sorted_emails = emails.get_emails_by_date(date=date, condition=condition) print(len(sorted_emails))

Get email list before a certain date

condition = "before" sorted_emails = emails.get_emails_by_date(date=date, condition=condition) print(len(sorted_emails))

Get email list after or on a certain date

condition = "after or on" sorted_emails = emails.get_emails_by_date(date=date, condition=condition) print(len(sorted_emails))

Output

Email graph plot

sorted_emails