diff --git a/email_handler.py b/email_handler.py index 9f602995..ae7cda0f 100644 --- a/email_handler.py +++ b/email_handler.py @@ -88,7 +88,7 @@ class MailHandler: user_email = gen_email.user.email - website_email = msg["From"] + website_email = get_email_part(msg["From"]) forward_email = ForwardEmail.get_by( gen_email_id=gen_email.id, website_email=website_email @@ -253,6 +253,17 @@ def get_email_name(email_from): return "" +def get_email_part(email_from): + """parse email from header and return the email part + First Last -> ab@cd.com + ab@cd.com -> "" + """ + if "<" in email_from: + return email_from[email_from.find("<") + 1 : email_from.find(">")].strip() + + return email_from + + if __name__ == "__main__": controller = Controller(MailHandler(), hostname="0.0.0.0", port=20381) diff --git a/tests/test_email_handler.py b/tests/test_email_handler.py index 4069c557..967c8713 100644 --- a/tests/test_email_handler.py +++ b/tests/test_email_handler.py @@ -1,4 +1,4 @@ -from email_handler import get_email_name +from email_handler import get_email_name, get_email_part def test_get_email_name(): @@ -6,3 +6,10 @@ def test_get_email_name(): assert get_email_name("First Last") == "First Last" assert get_email_name(" First Last ") == "First Last" assert get_email_name("ab@cd.com") == "" + + +def test_get_email_part(): + assert get_email_part("First Last ") == "ab@cd.com" + assert get_email_part("First Last") == "ab@cd.com" + assert get_email_part(" First Last ") == "ab@cd.com" + assert get_email_part("ab@cd.com") == "ab@cd.com"