2019-11-07 17:49:26 +01:00
|
|
|
"""
|
2019-11-08 11:05:34 +01:00
|
|
|
Handle the email *forward* and *reply*. phase. There are 3 actors:
|
2019-11-07 17:49:26 +01:00
|
|
|
- website: who sends emails to alias@sl.co address
|
|
|
|
- SL email handler (this script)
|
2019-11-08 11:05:34 +01:00
|
|
|
- user personal email: to be protected. Should never leak to website.
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
This script makes sure that in the forward phase, the email that is forwarded to user personal email has the following
|
|
|
|
envelope and header fields:
|
|
|
|
Envelope:
|
2019-11-21 13:42:48 +01:00
|
|
|
mail from: @website
|
2019-11-08 11:05:34 +01:00
|
|
|
rcpt to: @personal_email
|
2019-11-07 17:49:26 +01:00
|
|
|
Header:
|
|
|
|
From: @website
|
2019-11-08 11:05:34 +01:00
|
|
|
To: alias@sl.co # so user knows this email is sent to alias
|
|
|
|
Reply-to: special@sl.co # magic HERE
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
And in the reply phase:
|
|
|
|
Envelope:
|
2019-11-21 13:42:48 +01:00
|
|
|
mail from: @website
|
2019-11-07 17:49:26 +01:00
|
|
|
rcpt to: @website
|
|
|
|
|
|
|
|
Header:
|
2019-11-08 11:05:34 +01:00
|
|
|
From: alias@sl.co # so for website the email comes from alias. magic HERE
|
2019-11-07 17:49:26 +01:00
|
|
|
To: @website
|
|
|
|
|
|
|
|
The special@sl.co allows to hide user personal email when user clicks "Reply" to the forwarded email.
|
|
|
|
It should contain the following info:
|
|
|
|
- alias
|
|
|
|
- @website
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
import time
|
2019-11-19 10:23:06 +01:00
|
|
|
from email.message import EmailMessage
|
2019-11-07 17:49:26 +01:00
|
|
|
from email.parser import Parser
|
2019-11-18 14:14:01 +01:00
|
|
|
from email.policy import SMTPUTF8
|
2019-11-07 17:49:26 +01:00
|
|
|
from smtplib import SMTP
|
|
|
|
|
|
|
|
from aiosmtpd.controller import Controller
|
|
|
|
|
2019-11-20 18:52:49 +01:00
|
|
|
from app.config import EMAIL_DOMAIN, POSTFIX_SERVER, URL
|
2019-11-08 13:53:32 +01:00
|
|
|
from app.email_utils import notify_admin
|
2019-11-07 17:49:26 +01:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2019-11-16 17:07:59 +01:00
|
|
|
from app.models import GenEmail, ForwardEmail, ForwardEmailLog
|
2019-11-07 17:49:26 +01:00
|
|
|
from app.utils import random_words
|
2019-11-08 09:11:01 +01:00
|
|
|
from server import create_app
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MailHandler:
|
|
|
|
async def handle_DATA(self, server, session, envelope):
|
2019-11-08 10:51:30 +01:00
|
|
|
LOG.debug(">>> New message <<<")
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
LOG.debug("Mail from %s", envelope.mail_from)
|
|
|
|
LOG.debug("Rcpt to %s", envelope.rcpt_tos)
|
|
|
|
message_data = envelope.content.decode("utf8", errors="replace")
|
|
|
|
|
2019-11-08 10:51:30 +01:00
|
|
|
# Only when debug
|
|
|
|
# LOG.debug("Message data:\n")
|
|
|
|
# LOG.debug(message_data)
|
|
|
|
|
|
|
|
# host IP, setup via Docker network
|
2019-11-19 10:11:18 +01:00
|
|
|
smtp = SMTP(POSTFIX_SERVER, 25)
|
2019-11-18 14:14:01 +01:00
|
|
|
msg = Parser(policy=SMTPUTF8).parsestr(message_data)
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
if not envelope.rcpt_tos[0].startswith("reply+"): # Forward case
|
2019-11-19 21:47:58 +01:00
|
|
|
LOG.debug("Forward phase")
|
2019-11-08 09:11:01 +01:00
|
|
|
app = create_app()
|
|
|
|
|
|
|
|
with app.app_context():
|
2019-11-19 10:23:06 +01:00
|
|
|
return self.handle_forward(envelope, smtp, msg)
|
|
|
|
|
2019-11-07 17:49:26 +01:00
|
|
|
else:
|
|
|
|
LOG.debug("Reply phase")
|
2019-11-08 09:11:01 +01:00
|
|
|
app = create_app()
|
2019-11-07 17:49:26 +01:00
|
|
|
|
2019-11-08 09:11:01 +01:00
|
|
|
with app.app_context():
|
2019-11-19 10:23:06 +01:00
|
|
|
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")
|
|
|
|
return "510 Email not exist"
|
|
|
|
|
2019-11-21 13:58:06 +01:00
|
|
|
user_email = gen_email.user.email
|
|
|
|
|
2019-12-09 22:26:20 +01:00
|
|
|
website_email = get_email_part(msg["From"])
|
2019-11-19 10:23:06 +01:00
|
|
|
|
|
|
|
forward_email = ForwardEmail.get_by(
|
|
|
|
gen_email_id=gen_email.id, website_email=website_email
|
|
|
|
)
|
|
|
|
if not forward_email:
|
|
|
|
LOG.debug(
|
|
|
|
"create forward email for alias %s and website email %s",
|
|
|
|
alias,
|
|
|
|
website_email,
|
|
|
|
)
|
|
|
|
# todo: make sure reply_email is unique
|
|
|
|
reply_email = f"reply+{random_words()}@{EMAIL_DOMAIN}"
|
|
|
|
forward_email = ForwardEmail.create(
|
|
|
|
gen_email_id=gen_email.id,
|
|
|
|
website_email=website_email,
|
|
|
|
reply_email=reply_email,
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
forward_log = ForwardEmailLog.create(forward_id=forward_email.id)
|
|
|
|
|
|
|
|
if gen_email.enabled:
|
|
|
|
# add custom header
|
2019-11-21 13:58:06 +01:00
|
|
|
add_or_replace_header(msg, "X-SimpleLogin-Type", "Forward")
|
2019-11-19 10:23:06 +01:00
|
|
|
|
|
|
|
# remove reply-to header if present
|
|
|
|
if msg["Reply-To"]:
|
|
|
|
LOG.d("Delete reply-to header %s", msg["Reply-To"])
|
|
|
|
del msg["Reply-To"]
|
|
|
|
|
2019-11-19 21:47:58 +01:00
|
|
|
# change the from header so the sender comes from @SL
|
2019-11-19 10:23:06 +01:00
|
|
|
# so it can pass DMARC check
|
2019-11-19 21:47:58 +01:00
|
|
|
# replace the email part in from: header
|
|
|
|
from_header = (
|
|
|
|
get_email_name(msg["From"])
|
|
|
|
+ " - "
|
|
|
|
+ website_email.replace("@", " at ")
|
|
|
|
+ f" <{forward_email.reply_email}>"
|
|
|
|
)
|
2019-11-19 10:23:06 +01:00
|
|
|
msg.replace_header("From", from_header)
|
2019-11-19 21:47:58 +01:00
|
|
|
LOG.d("new from header:%s", from_header)
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2019-11-20 18:52:49 +01:00
|
|
|
# add List-Unsubscribe header
|
|
|
|
unsubscribe_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}"
|
|
|
|
add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
|
2019-11-21 13:58:06 +01:00
|
|
|
add_or_replace_header(
|
|
|
|
msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
|
|
|
|
)
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2019-11-21 14:59:57 +01:00
|
|
|
# remove DKIM-Signature as Postfix will add this header
|
|
|
|
if msg["DKIM-Signature"]:
|
|
|
|
LOG.d("Remove DKIM-Signature %s", msg["DKIM-Signature"])
|
|
|
|
del msg["DKIM-Signature"]
|
|
|
|
|
2019-11-20 18:52:49 +01:00
|
|
|
original_subject = msg["Subject"]
|
2019-11-19 10:23:06 +01:00
|
|
|
LOG.d(
|
|
|
|
"Forward mail from %s to %s, subject %s, mail_options %s, rcpt_options %s ",
|
|
|
|
website_email,
|
2019-11-21 13:58:06 +01:00
|
|
|
user_email,
|
2019-11-19 10:23:06 +01:00
|
|
|
original_subject,
|
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
|
|
|
)
|
|
|
|
|
2019-12-07 23:25:55 +01:00
|
|
|
# smtp.send_message has UnicodeEncodeErroremail issue
|
|
|
|
# encode message raw directly instead
|
|
|
|
msg_raw = msg.as_string().encode()
|
|
|
|
smtp.sendmail(
|
|
|
|
forward_email.reply_email,
|
|
|
|
user_email,
|
|
|
|
msg_raw,
|
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
2019-11-19 10:23:06 +01:00
|
|
|
)
|
2019-12-07 23:25:55 +01:00
|
|
|
|
|
|
|
# smtp.send_message(
|
|
|
|
# msg,
|
|
|
|
# from_addr=forward_email.reply_email,
|
|
|
|
# to_addrs=[user_email], # user personal email
|
|
|
|
# mail_options=envelope.mail_options,
|
|
|
|
# rcpt_options=envelope.rcpt_options,
|
|
|
|
# )
|
2019-11-19 10:23:06 +01:00
|
|
|
else:
|
|
|
|
LOG.d("%s is disabled, do not forward", gen_email)
|
|
|
|
forward_log.blocked = True
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
return "250 Message accepted for delivery"
|
|
|
|
|
|
|
|
def handle_reply(self, envelope, smtp, msg: EmailMessage) -> str:
|
|
|
|
reply_email = envelope.rcpt_tos[0]
|
|
|
|
|
2019-11-30 20:34:52 +01:00
|
|
|
# reply_email must end with EMAIL_DOMAIN
|
|
|
|
if not reply_email.endswith(EMAIL_DOMAIN):
|
|
|
|
LOG.error(f"Reply email {reply_email} has wrong domain")
|
|
|
|
return "550 wrong reply email"
|
|
|
|
|
2019-11-19 10:23:06 +01:00
|
|
|
forward_email = ForwardEmail.get_by(reply_email=reply_email)
|
2019-12-06 21:06:38 +01:00
|
|
|
alias: str = forward_email.gen_email.email
|
2019-11-19 10:23:06 +01:00
|
|
|
|
|
|
|
notify_admin(f"Reply phase used by user: {forward_email.gen_email.user.email} ")
|
|
|
|
|
2019-12-06 21:06:38 +01:00
|
|
|
# todo: add DKIM-Signature for custom domain
|
|
|
|
# remove DKIM-Signature for custom domain
|
|
|
|
if not alias.endswith(EMAIL_DOMAIN) and msg["DKIM-Signature"]:
|
|
|
|
LOG.d(
|
|
|
|
"Remove DKIM-Signature %s for custom-domain alias %s",
|
|
|
|
msg["DKIM-Signature"],
|
|
|
|
alias,
|
|
|
|
)
|
|
|
|
del msg["DKIM-Signature"]
|
|
|
|
|
2019-11-19 10:23:06 +01:00
|
|
|
# email seems to come from alias
|
|
|
|
msg.replace_header("From", alias)
|
|
|
|
msg.replace_header("To", forward_email.website_email)
|
|
|
|
|
2019-11-20 18:52:49 +01:00
|
|
|
# add List-Unsubscribe header
|
|
|
|
unsubscribe_link = f"{URL}/dashboard/unsubscribe/{forward_email.gen_email_id}"
|
|
|
|
add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
|
2019-11-21 13:58:06 +01:00
|
|
|
add_or_replace_header(
|
|
|
|
msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
|
|
|
|
)
|
2019-11-20 18:52:49 +01:00
|
|
|
|
2019-11-19 10:23:06 +01:00
|
|
|
LOG.d(
|
|
|
|
"send email from %s to %s, mail_options:%s,rcpt_options:%s",
|
|
|
|
alias,
|
|
|
|
forward_email.website_email,
|
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
|
|
|
)
|
|
|
|
|
|
|
|
smtp.send_message(
|
|
|
|
msg,
|
|
|
|
from_addr=alias,
|
|
|
|
to_addrs=[forward_email.website_email],
|
|
|
|
mail_options=envelope.mail_options,
|
|
|
|
rcpt_options=envelope.rcpt_options,
|
|
|
|
)
|
|
|
|
|
|
|
|
ForwardEmailLog.create(forward_id=forward_email.id, is_reply=True)
|
|
|
|
db.session.commit()
|
2019-11-16 17:07:59 +01:00
|
|
|
|
2019-11-07 17:49:26 +01:00
|
|
|
return "250 Message accepted for delivery"
|
|
|
|
|
|
|
|
|
2019-11-20 18:52:49 +01:00
|
|
|
def add_or_replace_header(msg: EmailMessage, header: str, value: str):
|
|
|
|
try:
|
|
|
|
msg.add_header(header, value)
|
|
|
|
except ValueError:
|
|
|
|
# the header exists already
|
|
|
|
msg.replace_header(header, value)
|
|
|
|
|
|
|
|
|
2019-11-19 21:47:58 +01:00
|
|
|
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 ""
|
|
|
|
|
|
|
|
|
2019-12-09 22:26:20 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-11-08 07:55:29 +01:00
|
|
|
if __name__ == "__main__":
|
2019-11-08 09:11:01 +01:00
|
|
|
controller = Controller(MailHandler(), hostname="0.0.0.0", port=20381)
|
2019-11-07 17:49:26 +01:00
|
|
|
|
2019-11-08 07:55:29 +01:00
|
|
|
controller.start()
|
2019-11-08 11:05:34 +01:00
|
|
|
LOG.d("Start mail controller %s %s", controller.hostname, controller.port)
|
2019-11-07 17:49:26 +01:00
|
|
|
|
2019-11-08 07:55:29 +01:00
|
|
|
while True:
|
2019-12-09 22:09:28 +01:00
|
|
|
time.sleep(2)
|