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

Gmail.jpg

Gmail - Automate response from keywords in mailbox

Give Feedback | Bug report

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

Author: Sanjay Sabu

Last update: 2023-05-18 (Created: 2021-04-20)

Description: This notebook automates the process of responding to emails in Gmail based on keywords found in the mailbox.

Input

Import librairies

import naas from naas_drivers import email from re import search

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.

  • keywords: This variable stores the keywords to be searched in email text.

  • cron: CRON to be set to schedule your notebook. More info here

  • email_to: This variable stores the emails to received the automatic response.

  • subject: This variable stores the subject of the email response.

  • content: This variable stores the content of the email response.

  • files: This variable stores the files to be sent as attachments.

# Inputs username = "xxxxx@xxxxx" password = naas.secret.get("GMAIL_APP_PASSWORD") or "xxxxxxxx" smtp_server = "imap.gmail.com" box = "INBOX" keywords = "sales report" # Outputs cron = "0 20 * * *" #everyday at 8PM email_to = "xxxx@xxxxx" subject = "Sales Report" content = "Hi \n,Here I am attaching the sales report as per your request\n.With Regards\n,NAAS Team" files = []

Model

Connect to email box

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

Get email list

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

Output

Automated reponse

for df in dataframe["text"]: text = df.lower() if search(keywords, text): naas.notifications.send( email_to=email_to, subject=subject, html=content, files=files )

Add scheduler

naas.scheduler.add(cron=cron)