diff --git a/app/config.py b/app/config.py index 8f8d2024..f4e2afe3 100644 --- a/app/config.py +++ b/app/config.py @@ -306,3 +306,5 @@ HOST = socket.gethostname() # by default use a tolerant score MAX_SPAM_SCORE = 10 SPAMASSASSIN_HOST = os.environ.get("SPAMASSASSIN_HOST") +# use a more restrictive score when replying +MAX_REPLY_PHASE_SPAM_SCORE = 5 diff --git a/email_handler.py b/email_handler.py index 7322aced..9b784d99 100644 --- a/email_handler.py +++ b/email_handler.py @@ -69,6 +69,7 @@ from app.config import ( SENDER_DIR, SPAMASSASSIN_HOST, MAX_SPAM_SCORE, + MAX_REPLY_PHASE_SPAM_SCORE, ) from app.email_utils import ( send_email, @@ -424,6 +425,7 @@ async def forward_email_to_mailbox( ) return False, "550 SL E14" + # Spam check spam_status = "" is_spam = False @@ -524,7 +526,7 @@ async def forward_email_to_mailbox( return True, "250 Message accepted for delivery" -def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> (bool, str): +async def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> (bool, str): """ return whether an email has been delivered and the smtp status ("250 Message accepted", "550 Non-existent email address", etc) @@ -577,6 +579,33 @@ def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> (bool, str # cannot use 4** here as sender will retry. 5** because that generates bounce report return True, "250 SL E11" + # Spam check + spam_status = "" + is_spam = False + + # do not use user.max_spam_score here + if SPAMASSASSIN_HOST: + spam_score = await get_spam_score(msg) + if spam_score > MAX_REPLY_PHASE_SPAM_SCORE: + is_spam = True + spam_status = "Spam detected by SpamAssassin server" + else: + is_spam, spam_status = get_spam_info(msg, max_score=MAX_REPLY_PHASE_SPAM_SCORE) + + if is_spam: + LOG.exception( + "Reply phase - email sent from %s to %s detected as spam", alias, contact + ) + email_log = EmailLog.create( + contact_id=contact.id, is_reply=True, user_id=contact.user_id + ) + email_log.is_spam = True + email_log.spam_status = spam_status + db.session.commit() + + handle_spam(contact, alias, msg, user, mailbox.email, email_log, is_reply=True) + return False, "550 SL E15 Email detected as spam" + delete_header(msg, _IP_HEADER) delete_header(msg, "DKIM-Signature") @@ -941,6 +970,7 @@ def handle_spam( user: User, mailbox_email: str, email_log: EmailLog, + is_reply=False, # whether the email is in forward or reply phase ): # Store the report & original email orig_msg = get_orig_message_from_spamassassin_report(msg) @@ -972,35 +1002,65 @@ def handle_spam( ) disable_alias_link = f"{URL}/dashboard/unsubscribe/{alias.id}" - # inform user - LOG.d( - "Inform user %s about spam email sent by %s to alias %s", - user, - contact.website_email, - alias.email, - ) - send_email_with_rate_control( - user, - ALERT_SPAM_EMAIL, - mailbox_email, - f"Email from {contact.website_email} to {alias.email} is detected as spam", - render( - "transactional/spam-email.txt", - name=user.name, - alias=alias, - website_email=contact.website_email, - disable_alias_link=disable_alias_link, - refused_email_url=refused_email_url, - ), - render( - "transactional/spam-email.html", - name=user.name, - alias=alias, - website_email=contact.website_email, - disable_alias_link=disable_alias_link, - refused_email_url=refused_email_url, - ), - ) + if is_reply: + LOG.d( + "Inform user %s about spam email sent from alias %s to %s", + user, + alias, + contact, + ) + send_email_with_rate_control( + user, + ALERT_SPAM_EMAIL, + mailbox_email, + f"Email from {contact.website_email} to {alias.email} is detected as spam", + render( + "transactional/spam-email-reply-phase.txt", + name=user.name, + alias=alias, + website_email=contact.website_email, + disable_alias_link=disable_alias_link, + refused_email_url=refused_email_url, + ), + render( + "transactional/spam-email-reply-phase.html", + name=user.name, + alias=alias, + website_email=contact.website_email, + disable_alias_link=disable_alias_link, + refused_email_url=refused_email_url, + ), + ) + else: + # inform user + LOG.d( + "Inform user %s about spam email sent by %s to alias %s", + user, + contact, + alias, + ) + send_email_with_rate_control( + user, + ALERT_SPAM_EMAIL, + mailbox_email, + f"Email from {contact.website_email} to {alias.email} is detected as spam", + render( + "transactional/spam-email.txt", + name=user.name, + alias=alias, + website_email=contact.website_email, + disable_alias_link=disable_alias_link, + refused_email_url=refused_email_url, + ), + render( + "transactional/spam-email.html", + name=user.name, + alias=alias, + website_email=contact.website_email, + disable_alias_link=disable_alias_link, + refused_email_url=refused_email_url, + ), + ) def handle_unsubscribe(envelope: Envelope): @@ -1112,7 +1172,7 @@ async def handle(envelope: Envelope, smtp: SMTP) -> str: LOG.debug( ">>> Reply phase %s(%s) -> %s", envelope.mail_from, msg["From"], rcpt_to ) - is_delivered, smtp_status = handle_reply(envelope, smtp, msg, rcpt_to) + is_delivered, smtp_status = await handle_reply(envelope, smtp, msg, rcpt_to) res.append((is_delivered, smtp_status)) else: # Forward case LOG.debug( diff --git a/templates/emails/transactional/spam-email-reply-phase.html b/templates/emails/transactional/spam-email-reply-phase.html new file mode 100644 index 00000000..3ec3c76c --- /dev/null +++ b/templates/emails/transactional/spam-email-reply-phase.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} + +{% block content %} + {{ render_text("Hi " + name) }} + {{ render_text("An email sent from your alias " + alias.email + " to " + website_email + " is detected as spam by our Spam Detection Engine (SpamAssassin).") }} + + {{ render_text('In most of the cases, the email will be refused by your contact.') }} + + {{ render_button("View the email", refused_email_url) }} + + {{ render_text('The email is automatically deleted in 7 days.') }} + + {{ render_text('Please let us know if you have any question by replying to this email.') }} + + {{ render_text('Thanks,
SimpleLogin Team.') }} + {{ raw_url(disable_alias_link) }} +{% endblock %} diff --git a/templates/emails/transactional/spam-email-reply-phase.txt b/templates/emails/transactional/spam-email-reply-phase.txt new file mode 100644 index 00000000..792db3f9 --- /dev/null +++ b/templates/emails/transactional/spam-email-reply-phase.txt @@ -0,0 +1,15 @@ +Hi {{name}} + +An email sent from your alias {{alias.email}} to {{website_email}} is detected as spam by our Spam Detection Engine (SpamAssassin). + +In most of the cases, the email will be refused by your contact. + +You can view this email here: +{{ refused_email_url }} + +The email is automatically deleted in 7 days. + +Please let us know if you have any question by replying to this email. + +Best, +SimpleLogin team. \ No newline at end of file