瀏覽代碼

put original name and email to from: header

Son NK 5 年之前
父節點
當前提交
75e07fff04
共有 2 個文件被更改,包括 29 次插入9 次删除
  1. 21 8
      email_handler.py
  2. 8 1
      tests/test_email_handler.py

+ 21 - 8
email_handler.py

@@ -86,7 +86,7 @@ class MailHandler:
         msg = Parser(policy=SMTPUTF8).parsestr(message_data)
 
         if not envelope.rcpt_tos[0].startswith("reply+"):  # Forward case
-            LOG.debug("Forward phase, add Reply-To header")
+            LOG.debug("Forward phase")
             app = create_app()
 
             with app.app_context():
@@ -146,17 +146,19 @@ class MailHandler:
                 LOG.d("Delete reply-to header %s", msg["Reply-To"])
                 del msg["Reply-To"]
 
-            # change the from header so the sender comes from @simplelogin
+            # change the from header so the sender comes from @SL
             # so it can pass DMARC check
-            from_header = f"Sender {website_email.replace('@', ' at ')} <{forward_email.reply_email}>"
+            # replace the email part in from: header
+            from_header = (
+                get_email_name(msg["From"])
+                + " - "
+                + website_email.replace("@", " at ")
+                + f" <{forward_email.reply_email}>"
+            )
             msg.replace_header("From", from_header)
+            LOG.d("new from header:%s", from_header)
 
-            # modify subject to let user know the email is forwarded from SL
             original_subject = msg["Subject"]
-            # msg.replace_header(
-            #     "Subject",
-            #     f"Forwarded by SimpleLogin. Subject: {original_subject}. From: {website_email}",
-            # )
 
             LOG.d(
                 "Forward mail from %s to %s, subject %s, mail_options %s, rcpt_options %s ",
@@ -215,6 +217,17 @@ class MailHandler:
         return "250 Message accepted for delivery"
 
 
+def get_email_name(email_from):
+    """parse email from header and return the name part
+    First Last <ab@cd.com> -> First Last
+    ab@cd.com -> ""
+    """
+    if "<" in email_from:
+        return email_from[: email_from.find("<")].strip()
+
+    return ""
+
+
 if __name__ == "__main__":
     controller = Controller(MailHandler(), hostname="0.0.0.0", port=20381)
 

+ 8 - 1
tests/test_email_handler.py

@@ -1,5 +1,12 @@
-from email_handler import parse_srs_email
+from email_handler import parse_srs_email, get_email_name
 
 
 def test_parse_srs_email():
     assert parse_srs_email("srs0=8lgw=y6=outlook.com=abcd@sl.co") == "abcd@outlook.com"
+
+
+def test_get_email_name():
+    assert get_email_name("First Last <ab@cd.com>") == "First Last"
+    assert get_email_name("First Last<ab@cd.com>") == "First Last"
+    assert get_email_name("  First Last   <ab@cd.com>") == "First Last"
+    assert get_email_name("ab@cd.com") == ""