44import email
55from email import policy
66import imaplib
7+ import logging
78import os
89import ssl
910
1314credential_path = "credentials.txt"
1415csv_path = "mails.csv"
1516
17+ logger = logging .getLogger ('imap_poller' )
18+
1619host = "imap.gmail.com"
1720port = 993
1821ssl_context = ssl .create_default_context ()
@@ -66,8 +69,8 @@ def write_to_csv(mail, writer):
6669 "utf-8"
6770 )
6871 email_text = get_text (email_body )
69- except Exception :
70- pass
72+ except Exception as exc :
73+ logger . warning ( 'Caught exception: %r' , exc )
7174 if (
7275 content_type == "text/plain"
7376 and "attachment" not in content_disposition
@@ -93,6 +96,8 @@ def write_to_csv(mail, writer):
9396def main ():
9497 mail , messages = connect_to_mailbox ()
9598
99+ logging .basicConfig (level = logging .WARNING )
100+
96101 total_no_of_mails = int (messages [0 ])
97102 # no. of latest mails to fetch
98103 # set it equal to total_no_of_emails to fetch all mail in the inbox
@@ -103,8 +108,8 @@ def main():
103108 writer .writerow (["Date" , "From" , "Subject" , "Text mail" ])
104109 try :
105110 write_to_csv (mail , writer )
106- except Exception as e :
107- print ( e )
111+ except Exception as exc :
112+ logger . warning ( 'Caught exception: %r' , exc )
108113
109114
110115if __name__ == "__main__" :
0 commit comments