notify user when emails sent to alias are bounced.

This commit is contained in:
Son NK 2020-02-22 20:57:19 +07:00
parent 85eca009ac
commit 78ef1cb8d4
5 changed files with 145 additions and 10 deletions

View File

@ -336,6 +336,7 @@ def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
return "550 alias unknown by SimpleLogin"
gen_email = forward_email.gen_email
user = gen_email.user
if gen_email.mailbox_id:
mailbox_email = gen_email.mailbox.email
else:
@ -352,17 +353,9 @@ def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
gen_email.user,
msg["From"],
)
# send the bounce email payload to admin
msg.replace_header("From", SUPPORT_EMAIL)
msg.replace_header("To", ADMIN_EMAIL)
add_dkim_signature(msg, get_email_domain_part(SUPPORT_EMAIL))
smtp.sendmail(
SUPPORT_EMAIL,
ADMIN_EMAIL,
msg.as_string().encode(),
envelope.mail_options,
envelope.rcpt_options,
handle_bounce(
alias, envelope, forward_email, gen_email, msg, smtp, user, mailbox_email
)
return "550 ignored"
@ -460,6 +453,77 @@ def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
return "250 Message accepted for delivery"
def handle_bounce(
alias, envelope, forward_email, gen_email, msg, smtp, user, mailbox_email
):
ForwardEmailLog.create(forward_id=forward_email.id, bounced=True)
db.session.commit()
nb_bounced = ForwardEmailLog.filter_by(
forward_id=forward_email.id, bounced=True
).count()
disable_alias_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}"
# inform user if this is the first bounced email
if nb_bounced == 1:
LOG.d(
"Inform user %s about bounced email sent by %s to alias %s",
user,
forward_email.website_from,
alias,
)
send_email(
mailbox_email,
f"Email from {forward_email.website_from} to {alias} cannot be delivered to your inbox",
render(
"transactional/bounced-email.txt",
name=user.name,
alias=alias,
website_from=forward_email.website_from,
website_email=forward_email.website_email,
disable_alias_link=disable_alias_link,
),
render(
"transactional/bounced-email.html",
name=user.name,
alias=alias,
website_from=forward_email.website_from,
website_email=forward_email.website_email,
disable_alias_link=disable_alias_link,
),
bounced_email=msg,
)
# disable the alias the second time email is bounced
elif nb_bounced >= 2:
LOG.d(
"Bounce happens again with alias %s from %s. Disable alias now ",
alias,
forward_email.website_from,
)
gen_email.enabled = False
db.session.commit()
send_email(
mailbox_email,
f"Alias {alias} has been disabled due to second undelivered email from {forward_email.website_from}",
render(
"transactional/automatic-disable-alias.txt",
name=user.name,
alias=alias,
website_from=forward_email.website_from,
website_email=forward_email.website_email,
),
render(
"transactional/automatic-disable-alias.html",
name=user.name,
alias=alias,
website_from=forward_email.website_from,
website_email=forward_email.website_email,
),
bounced_email=msg,
)
class MailHandler:
async def handle_DATA(self, server, session, envelope):
LOG.debug(">>> New message <<<")

View File

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block content %}
{{ render_text("Hi " + name) }}
{{ render_text("There are at least 2 emails sent to your alias <b>" + alias + "</b> from <b>" + website_email +
"</b> that have been <b>refused</b> (or bounced) by your email provider.") }}
{{ render_text("This is usually due to the email being considered as <b>spam</b> by your email provider.
The email is included at the end of this message so you can take a look at its content.") }}
{{ render_text('As security measure, we have <b>disabled</b> the alias ' + alias) }}
{{ render_text('Please let us know if you have any question.') }}
{{ render_text('Thanks, <br />SimpleLogin Team.') }}
{% endblock %}

View File

@ -0,0 +1,13 @@
Hi {{name}}
There are at least 2 emails sent to your alias {{alias}} from {{website_from}} that have been refused (or bounced) by your email provider.
This is usually due to the email being considered as spam by your email provider.
The email is included at the end of this message so you can take a look at its content.
As security measure, we have disabled the alias {{alias}}.
Please let us know if you have any question.
Best,
SimpleLogin team.

View File

@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block content %}
{{ render_text("Hi " + name) }}
{{ render_text("An email sent to your alias <b>" + alias + "</b> from <b>" + website_email + "</b> was <b>refused</b> (or <em>bounced</em>) by your email provider.") }}
{{ render_text("This is usually due to the email being considered as <b>spam</b> by your email provider. The email is included at the end of this message so you can take a look at its content.") }}
{{ render_text('To avoid spams forwarded by SimpleLogin server, please consider the following options:') }}
{{ render_text('1. If the email is not spam at all, it means your email provider has wrongly classified it as spam. In this case you can <b>create a filter to whitelist</b> it. The filter could be based on the sender, email subject, etc. As how to create the filter differs for each email provider, please check with your email provider on how to whitelist an email. Let us know if you need help to setup the filter by replying to this email.') }}
{{ render_text('2. If this email is indeed spam, it means your alias <b>' + alias + '</b> is now in the hands of a spammer. In this case, you should <b>disable</b> or delete the alias immediately. Or, do nothing and we\'ll <b>automatically</b> disable this alias the second time the email is refused. Don\'t worry, we\'ll send you another email when that happens.') }}
{{ render_button("Disable alias", disable_alias_link) }}
{{ render_text('Please let us know if you have any question.') }}
{{ render_text('Thanks, <br />SimpleLogin Team.') }}
{{ raw_url(disable_alias_link) }}
{% endblock %}

View File

@ -0,0 +1,21 @@
Hi {{name}}
An email sent to your alias {{alias}} from {{website_from}} was refused (or bounced) by your email provider.
This is usually due to the email being considered as spam by your email provider.
The email is included at the end of this message so you can take a look at its content.
To avoid spams forwarded by SimpleLogin server, please consider the following options:
1. If the email is not spam at all, it means your email provider has wrongly classified it as spam. In this case you can create a "filter" to whitelist it. The filter could be based on the sender, email subject, etc. As how to create the filter differs for each email provider, please check with your email provider on how to whitelist an email. Let us know if you need help to setup the filter by replying to this email.
2. If this email is spam, it means your alias {{alias}} is now in the hands of a spammer. In this case, you should disable or delete the alias immediately. Or, do nothing and we'll automatically disable this alias the second time the email is refused. Don't worry, we'll send you another email when that happens. You can disable the alias using this link:
{{disable_alias_link}}
Please let us know if you have any question.
Best,
SimpleLogin team.
---------------------------------------------------------------------
Below if the email that was refused: