diff --git a/email_handler.py b/email_handler.py index c0ba108a..a7263aed 100644 --- a/email_handler.py +++ b/email_handler.py @@ -33,7 +33,6 @@ It should contain the following info: import argparse import email -import time import uuid from email import encoders from email.encoders import encode_noop @@ -47,6 +46,7 @@ from typing import List, Tuple, Optional import newrelic.agent import sentry_sdk +import time from aiosmtpd.controller import Controller from aiosmtpd.smtp import Envelope from email_validator import validate_email, EmailNotValidError @@ -1059,6 +1059,11 @@ def handle_reply(envelope, msg: Message, rcpt_to: str) -> (bool, str): return False, status.E502 alias = contact.alias + + if alias.is_trashed(): + LOG.d("%s is trashed, do not forward", alias) + return False, status.E502 + alias_address: str = contact.alias.email alias_domain = get_email_domain_part(alias_address) diff --git a/tests/handler/test_trashed_alias.py b/tests/handler/test_trashed_alias.py new file mode 100644 index 00000000..cc1b4d89 --- /dev/null +++ b/tests/handler/test_trashed_alias.py @@ -0,0 +1,59 @@ +import random + +import arrow +from aiosmtpd.smtp import Envelope + +import email_handler +from app.email import status +from app.mail_sender import mail_sender +from app.models import Alias, BlockBehaviourEnum, Contact +from tests.utils import create_new_user, load_eml_file, random_email + + +@mail_sender.store_emails_test_decorator +def test_trash_on_forward(): + user = create_new_user() + user.block_behaviour = BlockBehaviourEnum.return_5xx + alias = Alias.create_new_random(user) + alias.delete_on = arrow.utcnow().shift(days=1) + envelope = Envelope() + envelope.mail_from = "env.somewhere" + envelope.rcpt_tos = [alias.email] + original_sender_address = random_email() + msg = load_eml_file( + "replacement_on_forward_phase.eml", + { + "sender_address": original_sender_address, + "recipient_address": alias.email, + "cc_address": random_email(), + }, + ) + result = email_handler.MailHandler()._handle(envelope, msg) + assert result == status.E502 + + +@mail_sender.store_emails_test_decorator +def test_trash_on_reply(): + user = create_new_user() + user.block_behaviour = BlockBehaviourEnum.return_5xx + alias = Alias.create_new_random(user) + alias.delete_on = arrow.utcnow().shift(days=1) + contact = Contact.create( + user_id=alias.user.id, + alias_id=alias.id, + website_email=f"contact{random.random()}@mailbox.lan", + reply_email=f"re-{random.random()}@sl.lan", + flush=True, + ) + envelope = Envelope() + envelope.mail_from = "env.somewhere" + envelope.rcpt_tos = [contact.reply_email] + msg = load_eml_file( + "replacement_on_reply_phase.eml", + { + "contact_reply_email": contact.reply_email, + "other_contact_reply_email": random_email(), + }, + ) + result = email_handler.MailHandler()._handle(envelope, msg) + assert result == status.E502