From c83b146f14f9b4e084ffde69a6288948a29d092f Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Mon, 11 Jan 2021 14:34:51 +0100 Subject: [PATCH] Add BOUNCE_PREFIX, BOUNCE_SUFFIX config --- app/config.py | 6 ++++++ app/email_utils.py | 4 ++++ example.env | 6 ++++++ tests/test_email_utils.py | 8 +++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/config.py b/app/config.py index 498a6210..681d08d2 100644 --- a/app/config.py +++ b/app/config.py @@ -54,6 +54,12 @@ EMAIL_DOMAIN = os.environ["EMAIL_DOMAIN"].lower() SUPPORT_EMAIL = os.environ["SUPPORT_EMAIL"] SUPPORT_NAME = os.environ.get("SUPPORT_NAME", "Son from SimpleLogin") ADMIN_EMAIL = os.environ.get("ADMIN_EMAIL") + +# VERP: mail_from set to BOUNCE_PREFIX + email_log.id + BOUNCE_SUFFIX +BOUNCE_PREFIX = os.environ.get("BOUNCE_PREFIX") or "bounce+" +BOUNCE_SUFFIX = os.environ.get("BOUNCE_SUFFIX") or f"+{EMAIL_DOMAIN}" +BOUNCE_EMAIL = BOUNCE_PREFIX + "{}" + BOUNCE_SUFFIX + try: MAX_NB_EMAIL_FREE_PLAN = int(os.environ["MAX_NB_EMAIL_FREE_PLAN"]) except Exception: diff --git a/app/email_utils.py b/app/email_utils.py index 0fa54444..03cdcad2 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -937,3 +937,7 @@ def should_disable(alias: Alias) -> bool: if nb_bounced_last_24h > 5: return True return False + + +def parse_email_log_id_from_bounce(email_address: str) -> int: + return int(email_address[email_address.find("+") : email_address.rfind("+")]) diff --git a/example.env b/example.env index 8a17e693..165587bd 100644 --- a/example.env +++ b/example.env @@ -39,6 +39,12 @@ SUPPORT_NAME=Son from SimpleLogin # in case sender is different than SUPPORT_EMAIL SENDER=sender@sl.local +# To use VERP +# prefix must end with + and suffix must start with + +# BOUNCE_PREFIX = "bounces+" +# BOUNCE_SUFFIX = "+@sl.local" + + # all emails sent to sender are stored in this folder SENDER_DIR=/tmp diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index 6e9b92be..6f51b2a5 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -1,7 +1,7 @@ import email from email.message import EmailMessage -from app.config import MAX_ALERT_24H, EMAIL_DOMAIN +from app.config import MAX_ALERT_24H, EMAIL_DOMAIN, BOUNCE_EMAIL from app.email_utils import ( get_email_domain_part, can_create_directory_for_address, @@ -24,6 +24,7 @@ from app.email_utils import ( replace, should_disable, decode_text, + parse_email_log_id_from_bounce, ) from app.extensions import db from app.models import User, CustomDomain, Alias, Contact, EmailLog @@ -615,3 +616,8 @@ def test_should_disable(flask_client): alias2 = Alias.create_new_random(user) db.session.commit() assert not should_disable(alias2) + + +def test_parse_email_log_id_from_bounce(): + assert parse_email_log_id_from_bounce("bounces+1234+@local") == 1234 + assert parse_email_log_id_from_bounce(BOUNCE_EMAIL.format(1234)) == 1234