<jupyter_start><jupyter_text>Gmail - Clean mailbox **Tags:** gmail productivity naas_drivers **Author:** [<NAME>](https://www.linkedin.com/in/martindonadieu)  Input  Import libraries<jupyter_code>import naas
from naas_drivers import email
import pandas as pd
import numpy as np
import plotly.express as px<jupyter_output><empty_output><jupyter_text>Account credentials<jupyter_code>username = "**********<EMAIL>"
password = "**********"
smtp_server = "imap.gmail.com"
box = "INBOX"<jupyter_output><empty_output><jupyter_text>Note: You need to create an application password following this procedure - https://support.google.com/mail/answer/185833?hl=en  Model  Connect to email box<jupyter_code>emails = naas_drivers.email.connect(username, 
        password, 
        username, 
        smtp_server)<jupyter_output><empty_output><jupyter_text>Get email list<jupyter_code>dataframe = emails.get(criteria="ALL")
dataframe<jupyter_output><empty_output><jupyter_text>Creating dataframe and inserting values<jupyter_code>sender_name = []
sender_email = []
for df in dataframe["from"]:
    sender_name.append(df['name'])
    sender_email.append(df['email'])
result = pd.DataFrame(columns = ['SENDER_NAME','SENDER_EMAIL','COUNT','PERCENTAGE'])
indexes = np.unique(sender_name, return_index=True)[1]
[sender_name[index] for index in sorted(indexes)]

indexes = np.unique(sender_email, return_index=True)[1]
[sender_email[index] for index in sorted(indexes)]
total_email = len(emails.get(criteria="ALL"))
c = 0
for i in sender_email:
    new_row = {'SENDER_NAME':sender_name[c],'SENDER_EMAIL':i,'COUNT':sender_email.count(i),'PERCENTAGE':round(((sender_email.count(i))/total_email)*100)}
    result = result.append(new_row, ignore_index=True)
    c+=1
result = result.drop_duplicates()
result.sort_values(by=['COUNT'], inplace=True, ascending=False)
result<jupyter_output><empty_output><jupyter_text>Email graph plot<jupyter_code>fig = px.bar(x=result['COUNT'], y=result['SENDER_EMAIL'], orientation='h')
fig.show()<jupyter_output><empty_output><jupyter_text>Deleting using uid<jupyter_code>%%time
uid = [21]   #uid of mails to be deleted
uid = map(str, uid)  
flag = "DELETED"
for i in uid:
    attachments = emails.set_flag(i, flag, True)<jupyter_output><empty_output><jupyter_text>Output  Deleting using email id<jupyter_code>d_email = "<EMAIL>"  # email id to be deleted
data_from = dataframe['from']
data_uid = dataframe['uid']
uid = []
for i in range(len(dataframe)):
    if data_from[i]['email'] == d_email:
        uid.append(data_uid[i])<jupyter_output><empty_output><jupyter_text>Deleting the emails<jupyter_code>for i in uid:
    attachments = emails.set_flag(i, "DELETED", True)<jupyter_output><empty_output><jupyter_text>Showing the upated email list<jupyter_code>dataframe = emails.get(criteria="ALL")
dataframe<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1426062521952933, 'fraction_numerical': 0.005268703898840885, 'mean_word_length': 7.63030303030303, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '20274', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1017, 'n_words': 177, 'path': 'Gmail/Gmail_Clean_mailbox.ipynb', 'repo_name': 'srini047/awesome-notebooks', 'size': '2873'}