use parseaddr instead of get_email_part

This commit is contained in:
Son NK 2020-03-15 22:32:48 +01:00 committed by Son NK
parent 7b90962076
commit 00f3b7b532
5 changed files with 11 additions and 41 deletions

View file

@ -1,26 +1,18 @@
from email.utils import parseaddr
from flask import g
from flask import jsonify, request
from flask import jsonify
from flask import request
from flask_cors import cross_origin
from app.api.base import api_bp, verify_api_key
from app.config import EMAIL_DOMAIN
from app.config import PAGE_LIMIT
from app.dashboard.views.alias_log import get_alias_log
from app.dashboard.views.index import get_alias_info, AliasInfo
from app.extensions import db
from app.models import GenEmail, ForwardEmail, ForwardEmailLog
from app.utils import random_string
import re
from flask import render_template, request, redirect, url_for, flash
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, validators, ValidationError
from app.config import EMAIL_DOMAIN
from app.dashboard.base import dashboard_bp
from app.email_utils import get_email_part
from app.extensions import db
from app.log import LOG
from app.models import ForwardEmailLog
from app.models import GenEmail, ForwardEmail
from app.utils import random_string
@ -310,7 +302,7 @@ def create_contact_route(alias_id):
if not ForwardEmail.get_by(reply_email=reply_email):
break
website_email = get_email_part(contact_email)
_, website_email = parseaddr(contact_email)
# already been added
if ForwardEmail.get_by(gen_email_id=gen_email.id, website_email=website_email):

View file

@ -1,4 +1,5 @@
import re
from email.utils import parseaddr
from flask import render_template, request, redirect, url_for, flash
from flask_login import login_required, current_user
@ -7,7 +8,6 @@ from wtforms import StringField, validators, ValidationError
from app.config import EMAIL_DOMAIN
from app.dashboard.base import dashboard_bp
from app.email_utils import get_email_part
from app.extensions import db
from app.log import LOG
from app.models import GenEmail, ForwardEmail
@ -76,7 +76,7 @@ def alias_contact_manager(alias_id, forward_email_id=None):
if not ForwardEmail.get_by(reply_email=reply_email):
break
website_email = get_email_part(contact_email)
_, website_email = parseaddr(contact_email)
# already been added
if ForwardEmail.get_by(

View file

@ -1,7 +1,5 @@
import email
import os
from email.message import EmailMessage, Message
from email.mime.application import MIMEApplication
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
@ -233,17 +231,6 @@ def send_email(
smtp.sendmail(SUPPORT_EMAIL, to_email, msg_raw)
def get_email_part(email_from):
"""parse email from header and return the email part
First Last <ab@cd.com> -> ab@cd.com
ab@cd.com -> ""
"""
if "<" in email_from:
return email_from[email_from.find("<") + 1 : email_from.find(">")].strip()
return email_from
def get_email_local_part(email):
"""
Get the local part from email

View file

@ -54,7 +54,6 @@ from app.config import (
POSTFIX_SUBMISSION_TLS,
)
from app.email_utils import (
get_email_part,
send_email,
add_dkim_signature,
get_email_domain_part,
@ -214,7 +213,7 @@ def get_or_create_forward_email(
"""
website_from_header can be the full-form email, i.e. "First Last <email@example.com>"
"""
website_email = get_email_part(website_from_header)
_, website_email = parseaddr(website_from_header)
forward_email = ForwardEmail.get_by(
gen_email_id=gen_email.id, website_email=website_email
)

View file

@ -1,7 +1,6 @@
from email.message import EmailMessage
from app.email_utils import (
get_email_part,
get_email_domain_part,
email_belongs_to_alias_domains,
can_be_used_as_personal_email,
@ -12,13 +11,6 @@ from app.extensions import db
from app.models import User, CustomDomain
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"
assert get_email_part(" First Last <ab@cd.com>") == "ab@cd.com"
assert get_email_part("ab@cd.com") == "ab@cd.com"
def test_get_email_domain_part():
assert get_email_domain_part("ab@cd.com") == "cd.com"