瀏覽代碼

refactor: create generate_reply_email()

Son NK 5 年之前
父節點
當前提交
fee69d9546
共有 1 個文件被更改,包括 14 次插入8 次删除
  1. 14 8
      email_handler.py

+ 14 - 8
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"""