refactor email_handler: create handle_forward and handle_reply

This commit is contained in:
Son NK 2019-11-19 10:23:06 +01:00
parent 34cf72eaee
commit 7207312ec9
1 changed files with 111 additions and 103 deletions

View File

@ -31,6 +31,7 @@ It should contain the following info:
"""
import time
from email.message import EmailMessage
from email.parser import Parser
from email.policy import SMTPUTF8
from smtplib import SMTP
@ -86,11 +87,22 @@ class MailHandler:
if not envelope.rcpt_tos[0].startswith("reply+"): # Forward case
LOG.debug("Forward phase, add Reply-To header")
alias = envelope.rcpt_tos[0] # alias@SL
app = create_app()
with app.app_context():
return self.handle_forward(envelope, smtp, msg)
else:
LOG.debug("Reply phase")
app = create_app()
with app.app_context():
return self.handle_reply(envelope, smtp, msg)
def handle_forward(self, envelope, smtp, msg: EmailMessage) -> str:
"""return *status_code message*"""
alias = envelope.rcpt_tos[0] # alias@SL
gen_email = GenEmail.get_by(email=alias)
if not gen_email:
LOG.d("alias %s not exist")
@ -167,19 +179,15 @@ class MailHandler:
forward_log.blocked = True
db.session.commit()
else:
LOG.debug("Reply phase")
return "250 Message accepted for delivery"
def handle_reply(self, envelope, smtp, msg: EmailMessage) -> str:
reply_email = envelope.rcpt_tos[0]
app = create_app()
with app.app_context():
forward_email = ForwardEmail.get_by(reply_email=reply_email)
alias = forward_email.gen_email.email
notify_admin(
f"Reply phase used by user: {forward_email.gen_email.user.email} "
)
notify_admin(f"Reply phase used by user: {forward_email.gen_email.user.email} ")
# email seems to come from alias
msg.replace_header("From", alias)