make email lowercase before processing them

This commit is contained in:
Son NK 2020-01-04 10:25:19 +01:00
parent d6aa6e7b94
commit 0dc901d05d
1 changed files with 10 additions and 10 deletions

View File

@ -31,7 +31,7 @@ It should contain the following info:
""" """
import time import time
from email.message import EmailMessage from email.message import Message
from email.parser import Parser from email.parser import Parser
from email.policy import SMTPUTF8 from email.policy import SMTPUTF8
from smtplib import SMTP from smtplib import SMTP
@ -85,11 +85,11 @@ class MailHandler:
smtp = SMTP(POSTFIX_SERVER, 25) smtp = SMTP(POSTFIX_SERVER, 25)
msg = Parser(policy=SMTPUTF8).parsestr(message_data) msg = Parser(policy=SMTPUTF8).parsestr(message_data)
rcpt_to = envelope.rcpt_tos[0].lower()
# Reply case # Reply case
# reply+ or ra+ (reverse-alias) prefix # reply+ or ra+ (reverse-alias) prefix
if envelope.rcpt_tos[0].startswith("reply+") or envelope.rcpt_tos[0].startswith( if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
"ra+"
):
LOG.debug("Reply phase") LOG.debug("Reply phase")
app = new_app() app = new_app()
@ -102,9 +102,9 @@ class MailHandler:
with app.app_context(): with app.app_context():
return self.handle_forward(envelope, smtp, msg) return self.handle_forward(envelope, smtp, msg)
def handle_forward(self, envelope, smtp: SMTP, msg: EmailMessage) -> str: def handle_forward(self, envelope, smtp: SMTP, msg: Message) -> str:
"""return *status_code message*""" """return *status_code message*"""
alias = envelope.rcpt_tos[0] # alias@SL alias = envelope.rcpt_tos[0].lower() # alias@SL
gen_email = GenEmail.get_by(email=alias) gen_email = GenEmail.get_by(email=alias)
if not gen_email: if not gen_email:
@ -212,8 +212,8 @@ class MailHandler:
db.session.commit() db.session.commit()
return "250 Message accepted for delivery" return "250 Message accepted for delivery"
def handle_reply(self, envelope, smtp: SMTP, msg: EmailMessage) -> str: def handle_reply(self, envelope, smtp: SMTP, msg: Message) -> str:
reply_email = envelope.rcpt_tos[0] reply_email = envelope.rcpt_tos[0].lower()
# reply_email must end with EMAIL_DOMAIN # reply_email must end with EMAIL_DOMAIN
if not reply_email.endswith(EMAIL_DOMAIN): if not reply_email.endswith(EMAIL_DOMAIN):
@ -230,7 +230,7 @@ class MailHandler:
return "550 alias unknown by SimpleLogin" return "550 alias unknown by SimpleLogin"
user_email = forward_email.gen_email.user.email user_email = forward_email.gen_email.user.email
if envelope.mail_from != user_email: if envelope.mail_from.lower() != user_email.lower():
LOG.error( LOG.error(
f"Reply email can only be used by user email. Actual mail_from: %s. User email %s", f"Reply email can only be used by user email. Actual mail_from: %s. User email %s",
envelope.mail_from, envelope.mail_from,
@ -293,7 +293,7 @@ class MailHandler:
return "250 Message accepted for delivery" return "250 Message accepted for delivery"
def add_or_replace_header(msg: EmailMessage, header: str, value: str): def add_or_replace_header(msg: Message, header: str, value: str):
try: try:
msg.add_header(header, value) msg.add_header(header, value)
except ValueError: except ValueError: