save email whose bounce info can't be parsed for debugging

This commit is contained in:
Son 2021-11-02 14:32:16 +01:00
parent 4a5983993e
commit 52a19818b7
2 changed files with 19 additions and 3 deletions

View file

@ -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 ""

View file

@ -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)