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

Commit af96100

Browse files
committed
projects/Store_emails_in_csv/store_emails.py: don't silently eat exceptions
import logging; display a log message when something goes wrong
1 parent b0f8cda commit af96100

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

projects/Store_emails_in_csv/store_emails.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import email
55
from email import policy
66
import imaplib
7+
import logging
78
import os
89
import ssl
910

@@ -13,6 +14,8 @@
1314
credential_path = "credentials.txt"
1415
csv_path = "mails.csv"
1516

17+
logger = logging.getLogger('imap_poller')
18+
1619
host = "imap.gmail.com"
1720
port = 993
1821
ssl_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):
9396
def 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

110115
if __name__ == "__main__":

0 commit comments

Comments
 (0)