From 52a19818b7cd2337d1892292924e6a76efefb203 Mon Sep 17 00:00:00 2001 From: Son Date: Tue, 2 Nov 2021 14:32:16 +0100 Subject: [PATCH] save email whose bounce info can't be parsed for debugging --- app/email_utils.py | 15 +++++++++++++++ email_handler.py | 7 ++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/email_utils.py b/app/email_utils.py index b04d29a1..d8c87641 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -1350,3 +1350,18 @@ def parse_full_address(full_address) -> (str, str): raise ValueError return full_address.display_name, full_address.address + + +def save_email_for_debugging(msg: Message) -> str: + """Save email for debugging to temporary location + Return the file path + """ + if TEMP_DIR: + file_name = str(uuid.uuid4()) + ".eml" + with open(os.path.join(TEMP_DIR, file_name), "wb") as f: + f.write(msg.as_bytes()) + + LOG.d("email saved to %s", file_name) + return file_name + + return "" diff --git a/email_handler.py b/email_handler.py index 4a0c4b27..10115fe5 100644 --- a/email_handler.py +++ b/email_handler.py @@ -124,6 +124,7 @@ from app.email_utils import ( parse_full_address, get_orig_message_from_yahoo_complaint, get_mailbox_bounce_info, + save_email_for_debugging, ) from app.log import LOG, set_message_id from app.models import ( @@ -1261,7 +1262,7 @@ def handle_bounce_forward_phase(msg: Message, email_log: EmailLog): email=mailbox.email, info=bounce_info.as_bytes().decode(), commit=True ) else: - LOG.w("cannot get bounce info") + LOG.w("cannot get bounce info, debug at %s", save_email_for_debugging(msg)) Bounce.create(email=mailbox.email, commit=True) LOG.d( @@ -1489,7 +1490,7 @@ def handle_bounce_reply_phase(envelope, msg: Message, email_log: EmailLog): commit=True, ) else: - LOG.w("cannot get bounce info") + LOG.w("cannot get bounce info, debug at %s", save_email_for_debugging(msg)) Bounce.create(email=sanitize_email(contact.website_email), commit=True) # Store the bounced email @@ -1753,7 +1754,7 @@ def handle_transactional_bounce(envelope: Envelope, msg, rcpt_to): commit=True, ) else: - LOG.w("cannot get bounce info") + LOG.w("cannot get bounce info, debug at %s", save_email_for_debugging(msg)) Bounce.create(email=transactional.email, commit=True)