From 00f3b7b532caebdfc3ca8e1a3d79e018678831d6 Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 15 Mar 2020 22:32:48 +0100 Subject: [PATCH] use parseaddr instead of get_email_part --- app/api/views/alias.py | 22 +++++++------------- app/dashboard/views/alias_contact_manager.py | 4 ++-- app/email_utils.py | 15 +------------ email_handler.py | 3 +-- tests/test_email_utils.py | 8 ------- 5 files changed, 11 insertions(+), 41 deletions(-) diff --git a/app/api/views/alias.py b/app/api/views/alias.py index 25f0dfdc..c6c21550 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -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): diff --git a/app/dashboard/views/alias_contact_manager.py b/app/dashboard/views/alias_contact_manager.py index 2caf84be..0db9f93a 100644 --- a/app/dashboard/views/alias_contact_manager.py +++ b/app/dashboard/views/alias_contact_manager.py @@ -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( diff --git a/app/email_utils.py b/app/email_utils.py index af926480..8d6b857c 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -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 -> "" - """ - 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 diff --git a/email_handler.py b/email_handler.py index f4f2e4f5..231e75b2 100644 --- a/email_handler.py +++ b/email_handler.py @@ -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 " """ - 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 ) diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index bc235b7f..fe013150 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -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" - 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" - - def test_get_email_domain_part(): assert get_email_domain_part("ab@cd.com") == "cd.com"