Merge pull request #873 from simple-login/ac-save-full-envelope-for-debug

Save original envelope for debugging
This commit is contained in:
Son Nguyen Kim 2022-04-06 17:55:31 +02:00 committed by GitHub
commit a611b90593
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View file

@ -6,6 +6,9 @@ import random
import time
import uuid
from copy import deepcopy
from aiosmtpd.smtp import Envelope
from email import policy, message_from_bytes, message_from_string
from email.header import decode_header, Header
from email.message import Message, EmailMessage
@ -1432,7 +1435,7 @@ def save_email_for_debugging(msg: Message, file_name_prefix=None) -> str:
if TEMP_DIR:
file_name = str(uuid.uuid4()) + ".eml"
if file_name_prefix:
file_name = file_name_prefix + file_name
file_name = "{}-{}".format(file_name_prefix, file_name)
with open(os.path.join(TEMP_DIR, file_name), "wb") as f:
f.write(msg.as_bytes())
@ -1443,6 +1446,24 @@ def save_email_for_debugging(msg: Message, file_name_prefix=None) -> str:
return ""
def save_envelope_for_debugging(envelope: Envelope, file_name_prefix=None) -> str:
"""Save envelope for debugging to temporary location
Return the file path
"""
if TEMP_DIR:
file_name = str(uuid.uuid4()) + ".eml"
if file_name_prefix:
file_name = "{}-{}".format(file_name_prefix, file_name)
with open(os.path.join(TEMP_DIR, file_name), "wb") as f:
f.write(envelope.original_content)
LOG.d("envelope saved to %s", file_name)
return file_name
return ""
def get_spamd_result(msg: Message) -> Optional[SpamdResult]:
spam_result_header = msg.get_all(headers.SPAMD_RESULT)
if not spam_result_header:

View file

@ -131,6 +131,7 @@ from app.email_utils import (
get_mailbox_bounce_info,
save_email_for_debugging,
get_spamd_result,
save_envelope_for_debugging,
)
from app.errors import (
NonReverseAliasInReplyPhase,
@ -2584,8 +2585,8 @@ class MailHandler:
envelope.rcpt_tos,
msg[headers.FROM],
msg[headers.TO],
save_email_for_debugging(
msg, file_name_prefix=e.__class__.__name__
save_envelope_for_debugging(
envelope, file_name_prefix=e.__class__.__name__
), # todo: remove
)
return status.E404