diff --git a/app/email_utils.py b/app/email_utils.py index ef7baa87..c5c21785 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -37,7 +37,6 @@ from sqlalchemy import func from app.config import ( ROOT_DIR, POSTFIX_SERVER, - NOT_SEND_EMAIL, DKIM_SELECTOR, DKIM_PRIVATE_KEY, ALIAS_DOMAINS, @@ -295,15 +294,6 @@ def send_email( from_addr=None, ): to_email = sanitize_email(to_email) - if NOT_SEND_EMAIL: - LOG.d( - "send email with subject '%s' to '%s', plaintext: %s, html: %s", - subject, - to_email, - plaintext, - html, - ) - return LOG.d("send email to %s, subject '%s'", to_email, subject) diff --git a/app/mail_sender.py b/app/mail_sender.py index 41c88a14..b8e38221 100644 --- a/app/mail_sender.py +++ b/app/mail_sender.py @@ -2,17 +2,12 @@ import time from concurrent.futures import ThreadPoolExecutor from mailbox import Message from smtplib import SMTP, SMTPServerDisconnected, SMTPRecipientsRefused -from typing import Optional, Dict +from typing import Optional, Dict, List import newrelic.agent from attr import dataclass -from app.config import ( - NOT_SEND_EMAIL, - POSTFIX_SUBMISSION_TLS, - POSTFIX_PORT, - POSTFIX_SERVER, -) +from app import config from app.email import headers from app.log import LOG from app.message_utils import message_to_bytes @@ -32,13 +27,26 @@ class SendRequest: class MailSender: def __init__(self): self._pool: Optional[ThreadPoolExecutor] = None + self._store_emails = False + self._emails_sent: List[SendRequest] = [] + + def store_emails_instead_of_sending(self): + self._store_emails = True + + def purge_stored_emails(self): + self._emails_sent = [] + + def get_stored_emails(self) -> List[SendRequest]: + return self._emails_sent def enable_background_pool(self, max_workers=10): self._pool = ThreadPoolExecutor(max_workers=max_workers) def send(self, send_request: SendRequest, retries: int = 2): """replace smtp.sendmail""" - if NOT_SEND_EMAIL: + if self._store_emails: + self._emails_sent.append(send_request) + if config.NOT_SEND_EMAIL: LOG.d( "send email with subject '%s', from '%s' to '%s'", send_request.msg[headers.SUBJECT], @@ -54,13 +62,13 @@ class MailSender: def _send_to_smtp(self, send_request: SendRequest, retries: int): try: start = time.time() - if POSTFIX_SUBMISSION_TLS: + if config.POSTFIX_SUBMISSION_TLS: smtp_port = 587 else: - smtp_port = POSTFIX_PORT + smtp_port = config.POSTFIX_PORT - with SMTP(POSTFIX_SERVER, smtp_port) as smtp: - if POSTFIX_SUBMISSION_TLS: + with SMTP(config.POSTFIX_SERVER, smtp_port) as smtp: + if config.POSTFIX_SUBMISSION_TLS: smtp.starttls() elapsed = time.time() - start diff --git a/tests/handler/test_provider_complaints.py b/tests/handler/test_provider_complaints.py index 8452cd71..7df5879e 100644 --- a/tests/handler/test_provider_complaints.py +++ b/tests/handler/test_provider_complaints.py @@ -13,6 +13,7 @@ from app.handler.provider_complaint import ( handle_hotmail_complaint, handle_yahoo_complaint, ) +from app.mail_sender import mail_sender from app.models import ( Alias, ProviderComplaint, @@ -61,6 +62,8 @@ def prepare_complaint( @pytest.mark.parametrize("handle_ftor,provider", origins) def test_provider_to_user(flask_client, handle_ftor, provider): + mail_sender.store_emails_instead_of_sending() + mail_sender.purge_stored_emails() user = create_new_user() alias = Alias.create_new_random(user) Session.commit() @@ -70,6 +73,8 @@ def test_provider_to_user(flask_client, handle_ftor, provider): assert len(found) == 0 alerts = SentAlert.filter_by(user_id=user.id).all() assert len(alerts) == 1 + sent_mails = mail_sender.get_stored_emails() + assert len(sent_mails) == 1 assert alerts[0].alert_type == f"{ALERT_COMPLAINT_TRANSACTIONAL_PHASE}_{provider}" @@ -89,6 +94,8 @@ def test_provider_forward_phase(flask_client, handle_ftor, provider): @pytest.mark.parametrize("handle_ftor,provider", origins) def test_provider_reply_phase(flask_client, handle_ftor, provider): + mail_sender.store_emails_instead_of_sending() + mail_sender.purge_stored_emails() user = create_new_user() alias = Alias.create_new_random(user) Session.commit() @@ -98,4 +105,6 @@ def test_provider_reply_phase(flask_client, handle_ftor, provider): assert len(found) == 0 alerts = SentAlert.filter_by(user_id=user.id).all() assert len(alerts) == 1 + sent_mails = mail_sender.get_stored_emails() + assert len(sent_mails) == 1 assert alerts[0].alert_type == f"{ALERT_COMPLAINT_FORWARD_PHASE}_{provider}"