Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Store emails into csv files #153

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Store emails into csv files
  • Loading branch information
ShivSt committed Aug 10, 2020
commit b5bf86d41d282787a7294f9c55268495cefacbc0
2 changes: 2 additions & 0 deletions projects/store emails in csv/credentials.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yourEmailID
yourPassword
5 changes: 5 additions & 0 deletions projects/store emails in csv/mails.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Date,From,Subject,Text mail
"Mon, 10 Aug 2020 13:24:18 +0530",Sachin Raina <[email protected]>,For testing emails fetching from python imaplib and email libraries ,"Hi, this is body text and i am alson attaching an attachment.
Thanks"
"Mon, 10 Aug 2020 05:32:03 +0000","""[email protected]"" <[email protected]>",Testing purposes,"Hi check if you are able to get plain text out of thia one.
Thanks"
1 change: 1 addition & 0 deletions projects/store emails in csv/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bs4
106 changes: 106 additions & 0 deletions projects/store emails in csv/store_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python

import imaplib
import email
from email import policy
import csv
import ssl
import os
from bs4 import BeautifulSoup

credential_path = os.getcwd() + "/credentials.txt"
csv_path = os.getcwd() + "/mails.csv"

host = "imap.gmail.com"
port = 993
ssl_context = ssl.create_default_context()


def connect_to_mailbox():
# get mail connection
mail = imaplib.IMAP4_SSL(host, port, ssl_context=ssl_context)

with open(credential_path, "rt") as fr:
user = fr.readline().strip()
pw = fr.readline().strip()
mail.login(user, pw)

# get mail box response and select a mail box
status, messages = mail.select("INBOX")
return mail, messages


# get plain text out of html mails
def get_text(email_body):
soup = BeautifulSoup(email_body, "lxml")
return soup.get_text(separator="\n", strip=True)


def write_to_csv(mail, writer):

for i in range(total_no_of_mails, total_no_of_mails - N, -1):
res, data = mail.fetch(str(i), "(RFC822)")

response = data[0]
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1], policy=policy.default)

# get header data
email_subject = msg["subject"]
email_from = msg["from"]
email_date = msg["date"]
email_text = ""

# 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 email_body
email_body = part.get_payload(decode=True).decode(
"utf-8"
)
email_text = get_text(email_body)
except Exception:
pass
if (
content_type == "text/plain"
and "attachment" not in content_disposition
):
# print text/plain emails and skip attachments
# print(email_text)
pass
elif "attachment" in content_disposition:
pass

else:
# extract content type of email
content_type = msg.get_content_type()
# get the email email_body
email_body = msg.get_payload(decode=True).decode("utf-8")
email_text = get_text(email_body)

# Write data in the csv file
row = [email_date, email_from, email_subject, email_text]
writer.writerow(row)


if __name__ == "__main__":

mail, messages = connect_to_mailbox()

total_no_of_mails = int(messages[0])
# no. of latest mails to fetch
# set it equal to total_no_of_emails to fetch all mail in the inbox
N = 2

with open(csv_path, "wt", encoding="utf-8", newline="") as fw:
writer = csv.writer(fw)
writer.writerow(["Date", "From", "Subject", "Text mail"])
try:
write_to_csv(mail, writer)
except Exception as e:
print(e)