1
1
#!/usr/bin/env python
2
2
3
- import imaplib
3
+ import csv
4
4
import email
5
5
from email import policy
6
- import csv
7
- import ssl
6
+ import imaplib
7
+ import logging
8
8
import os
9
+ import ssl
10
+
9
11
from bs4 import BeautifulSoup
10
12
11
- credential_path = os .getcwd () + "/credentials.txt"
12
- csv_path = os .getcwd () + "/mails.csv"
13
+
14
+ credential_path = "credentials.txt"
15
+ csv_path = "mails.csv"
16
+
17
+ logger = logging .getLogger ('imap_poller' )
13
18
14
19
host = "imap.gmail.com"
15
20
port = 993
@@ -36,7 +41,7 @@ def get_text(email_body):
36
41
return soup .get_text (separator = "\n " , strip = True )
37
42
38
43
39
- def write_to_csv (mail , writer ):
44
+ def write_to_csv (mail , writer , N , total_no_of_mails ):
40
45
41
46
for i in range (total_no_of_mails , total_no_of_mails - N , - 1 ):
42
47
res , data = mail .fetch (str (i ), "(RFC822)" )
@@ -60,12 +65,11 @@ def write_to_csv(mail, writer):
60
65
content_disposition = str (part .get ("Content-Disposition" ))
61
66
try :
62
67
# get the email email_body
63
- email_body = part .get_payload (decode = True ).decode (
64
- "utf-8"
65
- )
66
- email_text = get_text (email_body )
67
- except Exception :
68
- pass
68
+ email_body = part .get_payload (decode = True )
69
+ if email_body :
70
+ email_text = get_text (email_body .decode ('utf-8' ))
71
+ except Exception as exc :
72
+ logger .warning ('Caught exception: %r' , exc )
69
73
if (
70
74
content_type == "text/plain"
71
75
and "attachment" not in content_disposition
@@ -80,18 +84,22 @@ def write_to_csv(mail, writer):
80
84
# extract content type of email
81
85
content_type = msg .get_content_type ()
82
86
# get the email email_body
83
- email_body = msg .get_payload (decode = True ).decode ("utf-8" )
84
- email_text = get_text (email_body )
85
-
86
- # Write data in the csv file
87
- row = [email_date , email_from , email_subject , email_text ]
88
- writer .writerow (row )
89
-
90
-
91
- if __name__ == "__main__" :
87
+ email_body = msg .get_payload (decode = True )
88
+ if email_body :
89
+ email_text = get_text (email_body .decode ('utf-8' ))
90
+
91
+ if email_text is not None :
92
+ # Write data in the csv file
93
+ row = [email_date , email_from , email_subject , email_text ]
94
+ writer .writerow (row )
95
+ else :
96
+ logger .warning ('%s:%i: No message extracted' , "INBOX" , i )
92
97
98
+ def main ():
93
99
mail , messages = connect_to_mailbox ()
94
100
101
+ logging .basicConfig (level = logging .WARNING )
102
+
95
103
total_no_of_mails = int (messages [0 ])
96
104
# no. of latest mails to fetch
97
105
# set it equal to total_no_of_emails to fetch all mail in the inbox
@@ -101,6 +109,10 @@ def write_to_csv(mail, writer):
101
109
writer = csv .writer (fw )
102
110
writer .writerow (["Date" , "From" , "Subject" , "Text mail" ])
103
111
try :
104
- write_to_csv (mail , writer )
105
- except Exception as e :
106
- print (e )
112
+ write_to_csv (mail , writer , N , total_no_of_mails )
113
+ except Exception as exc :
114
+ logger .warning ('Caught exception: %r' , exc )
115
+
116
+
117
+ if __name__ == "__main__" :
118
+ main ()
0 commit comments