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

Gmail.jpg

Gmail - Mark emails as seen by dates

Give Feedback | Bug report

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

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

Description: This notebook goes through the emails within the date range set by the user and marks them all as seen.

Input

Import libraries

import naas import imaplib import email from datetime import date try: from imapclient import IMAPClient except: !pip install imapclient --user from imapclient import IMAPClient

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.

  • date: This variable stores the date that will be used to determine the range

  • condition: This variable stores the condition to get your emails

# Inputs username = "[email protected]" or "xxxxx@xxxx" # replace with your email password = naas.secret.get("GMAIL_APP_PASSWORD") smtp_server = "imap.gmail.com" # replace xxxx with the relevant server (eg. gmail) date = date(2023, 7, 20) # year, month, day condition = "ON" #"SINCE" or "BEFORE"

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 messages on conditions

all_messages = server.search([condition, date.strftime('%d-%b-%Y')]) print("✅ Messages fetched:", len(all_messages))

Output

Get emails on condition and mark them as seen

for message_id in all_messages: server.set_flags(message_id, [b'\\Seen']) print("✅ Emails have been marked as seen")