improve handle_bounce_reply_phase
This commit is contained in:
parent
3a03dec077
commit
3dee121bec
|
@ -1338,6 +1338,25 @@ def handle_bounce_reply_phase(alias: Alias, msg: Message, user: User):
|
||||||
Handle bounce that is sent to alias
|
Handle bounce that is sent to alias
|
||||||
Happens when an email cannot be sent from an alias to a contact
|
Happens when an email cannot be sent from an alias to a contact
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
|
email_log_id = int(get_header_from_bounce(msg, _EMAIL_LOG_ID_HEADER))
|
||||||
|
except Exception:
|
||||||
|
# save the data for debugging
|
||||||
|
file_path = f"/tmp/{random_string(10)}.eml"
|
||||||
|
with open(file_path, "wb") as f:
|
||||||
|
f.write(msg.as_bytes())
|
||||||
|
|
||||||
|
LOG.exception(
|
||||||
|
"Cannot get email-log-id from bounced report, %s %s %s",
|
||||||
|
alias,
|
||||||
|
user,
|
||||||
|
file_path,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
email_log = EmailLog.get(email_log_id)
|
||||||
|
contact = email_log.contact
|
||||||
|
|
||||||
# Store the bounced email
|
# Store the bounced email
|
||||||
# generate a name for the email
|
# generate a name for the email
|
||||||
random_name = str(uuid.uuid4())
|
random_name = str(uuid.uuid4())
|
||||||
|
@ -1346,44 +1365,34 @@ def handle_bounce_reply_phase(alias: Alias, msg: Message, user: User):
|
||||||
s3.upload_email_from_bytesio(full_report_path, BytesIO(msg.as_bytes()), random_name)
|
s3.upload_email_from_bytesio(full_report_path, BytesIO(msg.as_bytes()), random_name)
|
||||||
|
|
||||||
orig_msg = get_orig_message_from_bounce(msg)
|
orig_msg = get_orig_message_from_bounce(msg)
|
||||||
if not orig_msg:
|
file_path = None
|
||||||
# save the data for debugging
|
if orig_msg:
|
||||||
file_path = f"/tmp/{random_string(10)}.eml"
|
file_path = f"refused-emails/{random_name}.eml"
|
||||||
with open(file_path, "wb") as f:
|
s3.upload_email_from_bytesio(
|
||||||
f.write(msg.as_bytes())
|
file_path, BytesIO(orig_msg.as_bytes()), random_name
|
||||||
|
)
|
||||||
|
|
||||||
LOG.exception("Cannot parse bounce message, %s", file_path)
|
refused_email = RefusedEmail.create(
|
||||||
raise Exception("Cannot parse bounce message")
|
path=file_path, full_report_path=full_report_path, user_id=user.id, commit=True
|
||||||
|
)
|
||||||
|
LOG.d("Create refused email %s", refused_email)
|
||||||
|
|
||||||
file_path = f"refused-emails/{random_name}.eml"
|
email_log.bounced = True
|
||||||
s3.upload_email_from_bytesio(file_path, BytesIO(orig_msg.as_bytes()), random_name)
|
email_log.refused_email_id = refused_email.id
|
||||||
|
db.session.commit()
|
||||||
email_log_id = int(orig_msg[_EMAIL_LOG_ID_HEADER])
|
|
||||||
email_log = EmailLog.get(email_log_id)
|
|
||||||
contact = email_log.contact
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mailbox_id = int(orig_msg[_MAILBOX_ID_HEADER])
|
mailbox_id = int(get_header_from_bounce(msg, _MAILBOX_ID_HEADER))
|
||||||
except TypeError:
|
except Exception:
|
||||||
LOG.warning(
|
LOG.warning(
|
||||||
"cannot parse mailbox from original message header %s",
|
"cannot parse mailbox from bounce message report %s %s", alias, user
|
||||||
orig_msg[_MAILBOX_ID_HEADER],
|
|
||||||
)
|
)
|
||||||
# fall back to the default mailbox
|
# fall back to the default mailbox
|
||||||
mailbox = alias.mailbox
|
mailbox = alias.mailbox
|
||||||
else:
|
else:
|
||||||
mailbox = Mailbox.get(mailbox_id)
|
mailbox = Mailbox.get(mailbox_id)
|
||||||
email_log.bounced_mailbox_id = mailbox.id
|
email_log.bounced_mailbox_id = mailbox.id
|
||||||
|
db.session.commit()
|
||||||
refused_email = RefusedEmail.create(
|
|
||||||
path=file_path, full_report_path=full_report_path, user_id=user.id
|
|
||||||
)
|
|
||||||
db.session.flush()
|
|
||||||
LOG.d("Create refused email %s", refused_email)
|
|
||||||
|
|
||||||
email_log.bounced = True
|
|
||||||
email_log.refused_email_id = refused_email.id
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
refused_email_url = (
|
refused_email_url = (
|
||||||
URL + f"/dashboard/refused_email?highlight_id=" + str(email_log.id)
|
URL + f"/dashboard/refused_email?highlight_id=" + str(email_log.id)
|
||||||
|
|
Loading…
Reference in New Issue