From fee69d9546616400628f2328da060de32947b746 Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 22 Mar 2020 23:52:59 +0100 Subject: [PATCH] refactor: create generate_reply_email() --- email_handler.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/email_handler.py b/email_handler.py index 5a2e9b12..9847bcd8 100644 --- a/email_handler.py +++ b/email_handler.py @@ -226,14 +226,7 @@ def get_or_create_contact(website_from_header: str, alias: Alias) -> Contact: website_from_header, ) - # generate a reply_email, make sure it is unique - # not use while loop to avoid infinite loop - reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}" - for _ in range(1000): - if not Contact.get_by(reply_email=reply_email): - # found! - break - reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}" + reply_email = generate_reply_email() contact = Contact.create( user_id=alias.user_id, @@ -247,6 +240,19 @@ def get_or_create_contact(website_from_header: str, alias: Alias) -> Contact: return contact +def generate_reply_email(): + # generate a reply_email, make sure it is unique + # not use while loop to avoid infinite loop + reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}" + for _ in range(1000): + if not Contact.get_by(reply_email=reply_email): + # found! + break + reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}" + + return reply_email + + def should_append_alias(msg: Message, address: str): """whether an alias should be appened to TO header in message"""