use email.utils.parseaddr and formataddr instead of get_email_name

This commit is contained in:
Son NK 2020-03-15 22:29:53 +01:00 committed by Son NK
parent b4fd427008
commit 7b90962076
4 changed files with 15 additions and 37 deletions

View file

@ -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 <ab@cd.com> -> 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

View file

@ -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
@ -724,14 +724,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"""

View file

@ -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)

View file

@ -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 <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") == ""
def test_get_email_part():
assert get_email_part("First Last <ab@cd.com>") == "ab@cd.com"
assert get_email_part("First Last<ab@cd.com>") == "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"