diff --git a/app/email_utils.py b/app/email_utils.py index 2099d169..af926480 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -233,17 +233,6 @@ def send_email( smtp.sendmail(SUPPORT_EMAIL, to_email, msg_raw) -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 diff --git a/app/models.py b/app/models.py index 8b02d430..b4645d2a 100644 --- a/app/models.py +++ b/app/models.py @@ -1,6 +1,7 @@ import enum import random import uuid +from email.utils import parseaddr, formataddr import arrow import bcrypt @@ -17,7 +18,6 @@ from app.config import ( AVATAR_URL_EXPIRATION, JOB_ONBOARDING_1, ) - from app.extensions import db from app.log import LOG from app.oauth_models import Scope @@ -729,14 +729,16 @@ class ForwardEmail(db.Model, ModelMixin): def website_send_to(self): """return the email address with name. to use when user wants to send an email from the alias""" - from app.email_utils import get_email_name if self.website_from: - name = get_email_name(self.website_from) - if name: - return name + " " + self.website_email + f" <{self.reply_email}>" + website_name, _ = parseaddr(self.website_from) - return self.website_email.replace("@", " at ") + f" <{self.reply_email}>" + if website_name: + return formataddr( + (website_name + " " + self.website_email, self.reply_email) + ) + + return formataddr((self.website_email.replace("@", " at "), self.reply_email)) def last_reply(self) -> "ForwardEmailLog": """return the most recent reply""" diff --git a/email_handler.py b/email_handler.py index b98143b4..f4f2e4f5 100644 --- a/email_handler.py +++ b/email_handler.py @@ -30,14 +30,15 @@ It should contain the following info: """ -import uuid import time +import uuid from email import encoders from email.message import Message from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.parser import Parser from email.policy import SMTPUTF8 +from email.utils import parseaddr, formataddr from io import BytesIO from smtplib import SMTP from typing import Optional @@ -53,7 +54,6 @@ from app.config import ( POSTFIX_SUBMISSION_TLS, ) from app.email_utils import ( - get_email_name, get_email_part, send_email, add_dkim_signature, @@ -333,13 +333,13 @@ def handle_forward(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str: # so it can pass DMARC check # replace the email part in from: header website_from_header = msg["From"] - website_email = get_email_part(website_from_header) - from_header = ( - get_email_name(website_from_header) - + ("" if get_email_name(website_from_header) == "" else " - ") + website_name, website_email = parseaddr(website_from_header) + new_website_name = ( + website_name + + (" - " if website_name else "") + website_email.replace("@", " at ") - + f" <{forward_email.reply_email}>" ) + from_header = formataddr((new_website_name, forward_email.reply_email)) add_or_replace_header(msg, "From", from_header) LOG.d("new from header:%s", from_header) diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index 27e8b2a1..bc235b7f 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -1,9 +1,7 @@ from email.message import EmailMessage from app.email_utils import ( - get_email_name, get_email_part, - get_email_local_part, get_email_domain_part, email_belongs_to_alias_domains, can_be_used_as_personal_email, @@ -14,13 +12,6 @@ from app.extensions import db from app.models import User, CustomDomain -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(" 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" @@ -28,10 +19,6 @@ def test_get_email_part(): assert get_email_part("ab@cd.com") == "ab@cd.com" -def test_get_email_local_part(): - assert get_email_local_part("ab@cd.com") == "ab" - - def test_get_email_domain_part(): assert get_email_domain_part("ab@cd.com") == "cd.com"