diff --git a/app/email_utils.py b/app/email_utils.py index d4fd076b..40dc5353 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -134,3 +134,25 @@ def send_by_postfix(to_email, subject, plaintext, html): def notify_admin(subject, html_content=""): send_by_postfix(ADMIN_EMAIL, subject, html_content, html_content) + + +def get_email_name(email_from): + """parse email from header and return the name part + First Last -> First Last + ab@cd.com -> "" + """ + if "<" in email_from: + return email_from[: email_from.find("<")].strip() + + 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 diff --git a/email_handler.py b/email_handler.py index e7cd9a20..368c47c1 100644 --- a/email_handler.py +++ b/email_handler.py @@ -39,6 +39,7 @@ from smtplib import SMTP from aiosmtpd.controller import Controller from app.config import EMAIL_DOMAIN, POSTFIX_SERVER, URL +from app.email_utils import get_email_name, get_email_part from app.extensions import db from app.log import LOG from app.models import GenEmail, ForwardEmail, ForwardEmailLog @@ -257,28 +258,6 @@ def add_or_replace_header(msg: EmailMessage, header: str, value: str): msg.replace_header(header, value) -def get_email_name(email_from): - """parse email from header and return the name part - First Last -> First Last - ab@cd.com -> "" - """ - if "<" in email_from: - return email_from[: email_from.find("<")].strip() - - 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_utils.py similarity index 90% rename from tests/test_email_handler.py rename to tests/test_email_utils.py index 967c8713..47102e04 100644 --- a/tests/test_email_handler.py +++ b/tests/test_email_utils.py @@ -1,4 +1,4 @@ -from email_handler import get_email_name, get_email_part +from app.email_utils import get_email_name, get_email_part def test_get_email_name():