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

Gmail.jpg

Gmail - Read mailbox

Give Feedback | Bug report

Tags: #gmail #productivity #naas_drivers #operations #snippet #dataframe

Last update: 2023-05-12 (Created: 2021-04-15)

Description: This notebook allows you to read your Gmail inbox and returns a dataframe:

  • uid: This column represents the unique identifier associated with each row or email in the dataframe.

  • subject: The subject column contains the subject line or title of the email.

  • from: The "from" column contains information about the sender of the email. It includes the email address and name of the sender.

  • to: The "to" column contains information about the primary recipients of the email. It includes the email addresses and names of the recipients.

  • cc: The "cc" column represents the carbon copy recipients of the email. It contains a list of email addresses and names of the cc'd recipients.

  • bcc: The "bcc" column contains the blind carbon copy recipients of the email. Similar to the "cc" column, it includes a list of email addresses and names.

  • reply_to: This column contains the email addresses and names that should be used when replying to the email.

  • date: The "date" column indicates the date and time when the email was sent.

  • text: The "text" column contains the plain text content of the email.

  • html: The "html" column includes the HTML-formatted content of the email.

  • flags: The "flags" column represents any flags or indicators associated with the email, such as important, starred, etc. Possible value for flag: 'SEEN', 'ANSWERED', 'FLAGGED', 'DELETED', 'DRAFT', 'RECENT'

  • headers: This column contains additional headers of the email, such as the "delivered-to," "received," and "X-Google-Smtp-Source" headers.

  • size_rfc822: The "size_rfc822" column indicates the size of the email in RFC822 format.

  • size: The "size" column represents the size of the email in bytes.

  • obj: The "obj" column contains the object representation of the email.

  • attachments: This column includes any attachments associated with the email, such as files, images, etc.

Input

Import librairy

import naas from naas_drivers import email

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.

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

Model

Connect to email box

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

Output

Get email list

df = emails.get(box=box).sort_values(by="date", ascending=False) print(f"✅ Emails fetched:", len(df)) df