remove handle_bounce_deprecated
This commit is contained in:
parent
c927edfeaa
commit
d1d7a93ca5
152
email_handler.py
152
email_handler.py
|
@ -793,21 +793,6 @@ def handle_reply(envelope, msg: Message, rcpt_to: str) -> (bool, str):
|
||||||
)
|
)
|
||||||
return [(False, "550 SL E20 Account disabled")]
|
return [(False, "550 SL E20 Account disabled")]
|
||||||
|
|
||||||
# bounce email initiated by Postfix
|
|
||||||
# can happen in case emails cannot be delivered to user-email
|
|
||||||
# in this case Postfix will try to send a bounce report to original sender, which is
|
|
||||||
# the "reply email"
|
|
||||||
if mail_from == "<>":
|
|
||||||
LOG.warning(
|
|
||||||
"Bounce when sending to alias %s from %s, user %s",
|
|
||||||
alias,
|
|
||||||
contact,
|
|
||||||
user,
|
|
||||||
)
|
|
||||||
|
|
||||||
handle_bounce_deprecated(contact, alias, msg, user)
|
|
||||||
return False, "550 SL E6"
|
|
||||||
|
|
||||||
# Anti-spoofing
|
# Anti-spoofing
|
||||||
mailbox = get_mailbox_from_mail_from(mail_from, alias)
|
mailbox = get_mailbox_from_mail_from(mail_from, alias)
|
||||||
if not mailbox:
|
if not mailbox:
|
||||||
|
@ -1135,143 +1120,6 @@ def handle_unknown_mailbox(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def handle_bounce_deprecated(contact: Contact, alias: Alias, msg: Message, user: User):
|
|
||||||
"""
|
|
||||||
Handle bounce that is sent to the reverse-alias
|
|
||||||
Happens when an email cannot be forwarded to a mailbox
|
|
||||||
"""
|
|
||||||
LOG.exception("handle_bounce_deprecated shouldn't be called %s %s", contact, alias)
|
|
||||||
disable_alias_link = f"{URL}/dashboard/unsubscribe/{alias.id}"
|
|
||||||
|
|
||||||
# Store the bounced email
|
|
||||||
# generate a name for the email
|
|
||||||
random_name = str(uuid.uuid4())
|
|
||||||
|
|
||||||
full_report_path = f"refused-emails/full-{random_name}.eml"
|
|
||||||
s3.upload_email_from_bytesio(full_report_path, BytesIO(to_bytes(msg)), random_name)
|
|
||||||
|
|
||||||
file_path = None
|
|
||||||
|
|
||||||
orig_msg = get_orig_message_from_bounce(msg)
|
|
||||||
if not orig_msg:
|
|
||||||
# Some MTA does not return the original message in bounce message
|
|
||||||
# nothing we can do here
|
|
||||||
LOG.warning(
|
|
||||||
"Cannot parse original message from bounce message %s %s %s %s",
|
|
||||||
alias,
|
|
||||||
user,
|
|
||||||
contact,
|
|
||||||
full_report_path,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
file_path = f"refused-emails/{random_name}.eml"
|
|
||||||
s3.upload_email_from_bytesio(file_path, BytesIO(to_bytes(msg)), random_name)
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
# try to parse email_log
|
|
||||||
email_log = None
|
|
||||||
try:
|
|
||||||
email_log_id = int(get_header_from_bounce(msg, _EMAIL_LOG_ID_HEADER))
|
|
||||||
except Exception:
|
|
||||||
LOG.warning("cannot get email log id from bounce report, %s", refused_email)
|
|
||||||
else:
|
|
||||||
email_log = EmailLog.get(email_log_id)
|
|
||||||
|
|
||||||
# create new email_log if unable to parse from bounce report
|
|
||||||
if not email_log:
|
|
||||||
LOG.warning("cannot get the original email_log, create a new one")
|
|
||||||
email_log: EmailLog = EmailLog.create(
|
|
||||||
contact_id=contact.id, user_id=contact.user_id
|
|
||||||
)
|
|
||||||
|
|
||||||
# try to get mailbox
|
|
||||||
mailbox = None
|
|
||||||
if email_log:
|
|
||||||
mailbox = email_log.mailbox
|
|
||||||
|
|
||||||
# use the default mailbox if unable to parse from bounce report
|
|
||||||
if not mailbox:
|
|
||||||
LOG.warning("Use %s default mailbox %s", alias, refused_email)
|
|
||||||
mailbox = alias.mailbox
|
|
||||||
|
|
||||||
email_log.bounced = True
|
|
||||||
email_log.refused_email_id = refused_email.id
|
|
||||||
email_log.bounced_mailbox_id = mailbox.id
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
refused_email_url = f"{URL}/dashboard/refused_email?highlight_id={email_log.id}"
|
|
||||||
|
|
||||||
nb_bounced = EmailLog.filter_by(contact_id=contact.id, bounced=True).count()
|
|
||||||
if nb_bounced >= 2 and alias.cannot_be_disabled:
|
|
||||||
LOG.warning("%s cannot be disabled", alias)
|
|
||||||
|
|
||||||
# inform user of this bounce
|
|
||||||
if not should_disable(alias):
|
|
||||||
LOG.d(
|
|
||||||
"Inform user %s about a bounce from contact %s to alias %s",
|
|
||||||
user,
|
|
||||||
contact,
|
|
||||||
alias,
|
|
||||||
)
|
|
||||||
send_email_with_rate_control(
|
|
||||||
user,
|
|
||||||
ALERT_BOUNCE_EMAIL,
|
|
||||||
user.email,
|
|
||||||
f"Email from {contact.website_email} to {alias.email} cannot be delivered to your inbox",
|
|
||||||
render(
|
|
||||||
"transactional/bounce/bounced-email.txt",
|
|
||||||
alias=alias,
|
|
||||||
website_email=contact.website_email,
|
|
||||||
disable_alias_link=disable_alias_link,
|
|
||||||
refused_email_url=refused_email_url,
|
|
||||||
mailbox_email=mailbox.email,
|
|
||||||
),
|
|
||||||
render(
|
|
||||||
"transactional/bounce/bounced-email.html",
|
|
||||||
alias=alias,
|
|
||||||
website_email=contact.website_email,
|
|
||||||
disable_alias_link=disable_alias_link,
|
|
||||||
refused_email_url=refused_email_url,
|
|
||||||
mailbox_email=mailbox.email,
|
|
||||||
),
|
|
||||||
max_nb_alert=10,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
LOG.warning(
|
|
||||||
"Disable alias %s now",
|
|
||||||
alias,
|
|
||||||
)
|
|
||||||
alias.enabled = False
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
send_email_with_rate_control(
|
|
||||||
user,
|
|
||||||
ALERT_BOUNCE_EMAIL,
|
|
||||||
user.email,
|
|
||||||
f"Alias {alias.email} has been disabled due to second undelivered email from {contact.website_email}",
|
|
||||||
render(
|
|
||||||
"transactional/bounce/automatic-disable-alias.txt",
|
|
||||||
alias=alias,
|
|
||||||
website_email=contact.website_email,
|
|
||||||
refused_email_url=refused_email_url,
|
|
||||||
mailbox_email=mailbox.email,
|
|
||||||
),
|
|
||||||
render(
|
|
||||||
"transactional/bounce/automatic-disable-alias.html",
|
|
||||||
alias=alias,
|
|
||||||
website_email=contact.website_email,
|
|
||||||
refused_email_url=refused_email_url,
|
|
||||||
mailbox_email=mailbox.email,
|
|
||||||
),
|
|
||||||
max_nb_alert=10,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def handle_bounce_forward_phase(msg: Message, email_log: EmailLog):
|
def handle_bounce_forward_phase(msg: Message, email_log: EmailLog):
|
||||||
"""
|
"""
|
||||||
Handle forward phase bounce
|
Handle forward phase bounce
|
||||||
|
|
Loading…
Reference in New Issue