#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Nov 9 09:38:14 2021 @author: benjaminull """ import imaplib import email from email.header import decode_header import webbrowser import os import pandas as pd # account credentials username = "benjamin.ull.m@gmail.com" password = "madeskadakkjldjasdjaksdjlasdlsadldkasdakdllllllra1204" def clean(text): # clean text for creating a folder return "".join(c if c.isalnum() else "_" for c in text) # create an IMAP4 class with SSL imap = imaplib.IMAP4_SSL("imap.gmail.com") # authenticate password = password[0:4]+password[-6:len(password)] imap.login(username, password) status, messages = imap.select("INBOX") # number of top emails to fetch N = 2 # total number of emails messages = int(messages[0]) data_msg = pd.DataFrame() list_subject = [] list_body = [] for i in range(messages, messages-N, -1): # fetch the email message by ID res, msg = imap.fetch(str(i), "(RFC822)") try: for response in msg: if isinstance(response, tuple): # parse a bytes email into a message object msg = email.message_from_bytes(response[1]) # decode the email subject subject, encoding = decode_header(msg["Subject"])[0] if isinstance(subject, bytes): # if it's a bytes, decode to str subject = subject.decode(encoding) # decode email sender From, encoding = decode_header(msg.get("From"))[0] if isinstance(From, bytes): From = From.decode(encoding) list_subject.append(subject) # if the email message is multipart if msg.is_multipart(): # iterate over email parts for part in msg.walk(): # extract content type of email content_type = part.get_content_type() content_disposition = str(part.get("Content-Disposition")) try: # get the email body body = part.get_payload(decode=True).decode() except: pass if "attachment" in content_disposition: # download attachment filename = part.get_filename() if filename: folder_name = clean(subject) if not os.path.isdir(folder_name): # make a folder for this email (named after the subject) os.mkdir(folder_name) filepath = os.path.join(folder_name, filename) # download attachment and save it open(filepath, "wb").write(part.get_payload(decode=True)) else: # extract content type of email content_type = msg.get_content_type() # get the email body body = msg.get_payload(decode=True).decode() list_body.append(body) if content_type == "text/html": # if it's HTML, create a new HTML file and open it in browser folder_name = "mails/" + clean(subject) if not os.path.isdir(folder_name): # make a folder for this email (named after the subject) os.mkdir(folder_name) filename = "index.html" filepath = os.path.join(folder_name, filename) # write the file open(filepath, "w").write(body) # open in the default browser webbrowser.open(filepath) except: list_subject.append("") list_body.append("") pass data_msg["Subject"]=list_subject data_msg["Body"]=list_body print(data_msg) data_msg.to_csv("DataMensajes.csv") # close the connection and logout imap.close() imap.logout()