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
|
2020-01-04 10:25:19 +01:00
|
|
|
from email.message import Message
|
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
|
|
|
|
|
2020-01-22 10:22:59 +01:00
|
|
|
from app.config import EMAIL_DOMAIN, POSTFIX_SERVER, URL, ALIAS_DOMAINS
|
2019-12-17 17:48:06 +01:00
|
|
|
from app.email_utils import (
|
|
|
|
get_email_name,
|
|
|
|
get_email_part,
|
|
|
|
send_email,
|
|
|
|
add_dkim_signature,
|
2019-12-30 18:26:07 +01:00
|
|
|
get_email_domain_part,
|
2020-01-07 19:50:36 +01:00
|
|
|
add_or_replace_header,
|
|
|
|
delete_header,
|
2020-01-16 22:06:36 +01:00
|
|
|
send_cannot_create_directory_alias,
|
|
|
|
send_cannot_create_domain_alias,
|
2020-01-22 10:22:59 +01:00
|
|
|
email_belongs_to_alias_domains,
|
2020-02-11 16:46:53 +01:00
|
|
|
render,
|
2020-01-07 19:50:36 +01:00
|
|
|
)
|
2019-11-07 17:49:26 +01:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-01-30 08:43:31 +01:00
|
|
|
from app.models import (
|
|
|
|
GenEmail,
|
|
|
|
ForwardEmail,
|
|
|
|
ForwardEmailLog,
|
|
|
|
CustomDomain,
|
|
|
|
Directory,
|
|
|
|
User,
|
|
|
|
)
|
2019-12-15 17:04:46 +01:00
|
|
|
from app.utils import random_string
|
2019-11-08 09:11:01 +01:00
|
|
|
from server import create_app
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
|
2019-12-12 17:27:31 +01:00
|
|
|
# fix the database connection leak issue
|
|
|
|
# use this method instead of create_app
|
|
|
|
def new_app():
|
|
|
|
app = create_app()
|
|
|
|
|
|
|
|
@app.teardown_appcontext
|
|
|
|
def shutdown_session(response_or_exc):
|
|
|
|
# same as shutdown_session() in flask-sqlalchemy but this is not enough
|
|
|
|
db.session.remove()
|
|
|
|
|
|
|
|
# dispose the engine too
|
|
|
|
db.engine.dispose()
|
|
|
|
|
|
|
|
return 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
|
|
|
|
2020-01-04 10:25:19 +01:00
|
|
|
rcpt_to = envelope.rcpt_tos[0].lower()
|
|
|
|
|
2019-12-15 15:09:24 +01:00
|
|
|
# Reply case
|
|
|
|
# reply+ or ra+ (reverse-alias) prefix
|
2020-01-04 10:25:19 +01:00
|
|
|
if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
|
2019-12-15 15:09:24 +01:00
|
|
|
LOG.debug("Reply phase")
|
2019-12-12 17:27:31 +01:00
|
|
|
app = new_app()
|
2019-11-08 09:11:01 +01:00
|
|
|
|
|
|
|
with app.app_context():
|
2019-12-15 15:09:24 +01:00
|
|
|
return self.handle_reply(envelope, smtp, msg)
|
|
|
|
else: # Forward case
|
|
|
|
LOG.debug("Forward phase")
|
2019-12-12 17:27:31 +01:00
|
|
|
app = new_app()
|
2019-11-07 17:49:26 +01:00
|
|
|
|
2019-11-08 09:11:01 +01:00
|
|
|
with app.app_context():
|
2019-12-15 15:09:24 +01:00
|
|
|
return self.handle_forward(envelope, smtp, msg)
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-01-04 10:25:19 +01:00
|
|
|
def handle_forward(self, envelope, smtp: SMTP, msg: Message) -> str:
|
2019-11-19 10:23:06 +01:00
|
|
|
"""return *status_code message*"""
|
2020-01-04 10:25:19 +01:00
|
|
|
alias = envelope.rcpt_tos[0].lower() # alias@SL
|
2019-11-19 10:23:06 +01:00
|
|
|
|
|
|
|
gen_email = GenEmail.get_by(email=alias)
|
|
|
|
if not gen_email:
|
2020-01-16 22:06:36 +01:00
|
|
|
LOG.d(
|
|
|
|
"alias %s not exist. Try to see if it can be created on the fly", alias
|
|
|
|
)
|
2019-12-30 18:26:07 +01:00
|
|
|
|
2020-01-08 21:38:41 +01:00
|
|
|
# try to see if alias could be created on-the-fly
|
|
|
|
on_the_fly = False
|
|
|
|
|
2020-01-08 22:09:46 +01:00
|
|
|
# check if alias belongs to a directory, ie having directory/anything@EMAIL_DOMAIN format
|
2020-01-22 10:22:59 +01:00
|
|
|
if email_belongs_to_alias_domains(alias):
|
2020-01-19 22:06:36 +01:00
|
|
|
if "/" in alias or "+" in alias or "#" in alias:
|
|
|
|
if "/" in alias:
|
|
|
|
sep = "/"
|
|
|
|
elif "+" in alias:
|
|
|
|
sep = "+"
|
|
|
|
else:
|
|
|
|
sep = "#"
|
|
|
|
|
|
|
|
directory_name = alias[: alias.find(sep)]
|
2020-01-08 21:52:14 +01:00
|
|
|
LOG.d("directory_name %s", directory_name)
|
2020-01-08 21:38:41 +01:00
|
|
|
|
|
|
|
directory = Directory.get_by(name=directory_name)
|
2020-01-16 22:06:36 +01:00
|
|
|
|
2020-01-19 22:06:36 +01:00
|
|
|
# Only premium user can use the directory feature
|
2020-01-08 21:38:41 +01:00
|
|
|
if directory:
|
2020-01-30 08:43:31 +01:00
|
|
|
dir_user: User = directory.user
|
|
|
|
if dir_user.can_create_new_alias():
|
2020-01-16 22:06:36 +01:00
|
|
|
LOG.d("create alias %s for directory %s", alias, directory)
|
|
|
|
on_the_fly = True
|
|
|
|
|
|
|
|
gen_email = GenEmail.create(
|
|
|
|
email=alias,
|
|
|
|
user_id=directory.user_id,
|
|
|
|
directory_id=directory.id,
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
LOG.error(
|
|
|
|
"User %s is not premium anymore and cannot create alias with directory",
|
|
|
|
dir_user,
|
|
|
|
)
|
|
|
|
send_cannot_create_directory_alias(
|
|
|
|
dir_user, alias, directory_name
|
|
|
|
)
|
2020-01-20 23:01:40 +01:00
|
|
|
|
|
|
|
# try to create alias on-the-fly with custom-domain catch-all feature
|
|
|
|
# check if alias is custom-domain alias and if the custom-domain has catch-all enabled
|
|
|
|
if not on_the_fly:
|
2020-01-16 22:06:36 +01:00
|
|
|
alias_domain = get_email_domain_part(alias)
|
|
|
|
custom_domain = CustomDomain.get_by(domain=alias_domain)
|
|
|
|
|
|
|
|
# Only premium user can continue using the catch-all feature
|
|
|
|
if custom_domain and custom_domain.catch_all:
|
2020-01-30 08:43:31 +01:00
|
|
|
domain_user: User = custom_domain.user
|
|
|
|
if domain_user.can_create_new_alias():
|
2020-01-16 22:06:36 +01:00
|
|
|
LOG.d("create alias %s for domain %s", alias, custom_domain)
|
2020-01-08 21:38:41 +01:00
|
|
|
on_the_fly = True
|
|
|
|
|
|
|
|
gen_email = GenEmail.create(
|
|
|
|
email=alias,
|
2020-01-16 22:06:36 +01:00
|
|
|
user_id=custom_domain.user_id,
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
automatic_creation=True,
|
2020-01-08 21:38:41 +01:00
|
|
|
)
|
|
|
|
db.session.commit()
|
2020-01-16 22:06:36 +01:00
|
|
|
else:
|
|
|
|
LOG.error(
|
|
|
|
"User %s is not premium anymore and cannot create alias with domain %s",
|
|
|
|
domain_user,
|
|
|
|
alias_domain,
|
|
|
|
)
|
|
|
|
send_cannot_create_domain_alias(
|
|
|
|
domain_user, alias, alias_domain
|
|
|
|
)
|
2020-01-08 21:38:41 +01:00
|
|
|
|
|
|
|
if not on_the_fly:
|
2020-01-20 23:01:40 +01:00
|
|
|
LOG.d("alias %s cannot be created on-the-fly, return 510", alias)
|
2019-12-30 18:26:07 +01:00
|
|
|
return "510 Email not exist"
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-02-10 17:23:40 +01:00
|
|
|
if gen_email.mailbox_id:
|
|
|
|
mailbox_email = gen_email.mailbox.email
|
|
|
|
else:
|
|
|
|
mailbox_email = gen_email.user.email
|
2019-11-21 13:58:06 +01:00
|
|
|
|
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
|
|
|
|
)
|
2020-01-29 04:47:13 +01:00
|
|
|
if forward_email:
|
|
|
|
# update the From header if needed
|
|
|
|
if forward_email.website_from != msg["From"]:
|
|
|
|
LOG.d("Update From header for %s", forward_email)
|
|
|
|
forward_email.website_from = msg["From"]
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
2019-11-19 10:23:06 +01:00
|
|
|
LOG.debug(
|
|
|
|
"create forward email for alias %s and website email %s",
|
|
|
|
alias,
|
|
|
|
website_email,
|
|
|
|
)
|
2019-12-15 10:18:33 +01:00
|
|
|
|
|
|
|
# generate a reply_email, make sure it is unique
|
|
|
|
# not use while to avoid infinite loop
|
|
|
|
for _ in range(1000):
|
2019-12-15 16:14:33 +01:00
|
|
|
reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}"
|
2019-12-15 10:18:33 +01:00
|
|
|
if not ForwardEmail.get_by(reply_email=reply_email):
|
|
|
|
break
|
|
|
|
|
2019-11-19 10:23:06 +01:00
|
|
|
forward_email = ForwardEmail.create(
|
|
|
|
gen_email_id=gen_email.id,
|
|
|
|
website_email=website_email,
|
2019-12-09 22:40:49 +01:00
|
|
|
website_from=msg["From"],
|
2019-11-19 10:23:06 +01:00
|
|
|
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
|
2020-01-07 19:49:26 +01:00
|
|
|
delete_header(msg, "Reply-To")
|
2019-11-19 10:23:06 +01:00
|
|
|
|
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 = (
|
2019-12-15 15:09:24 +01:00
|
|
|
get_email_name(msg["From"])
|
2020-01-29 14:31:54 +01:00
|
|
|
+ ("" if get_email_name(msg["From"]) == "" else " - ")
|
2019-12-15 15:09:24 +01:00
|
|
|
+ website_email.replace("@", " at ")
|
|
|
|
+ f" <{forward_email.reply_email}>"
|
2019-11-19 21:47:58 +01:00
|
|
|
)
|
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-12-18 17:07:20 +01:00
|
|
|
add_dkim_signature(msg, EMAIL_DOMAIN)
|
|
|
|
|
2019-11-19 10:23:06 +01:00
|
|
|
LOG.d(
|
2019-12-18 17:07:20 +01:00
|
|
|
"Forward mail from %s to %s, mail_options %s, rcpt_options %s ",
|
2019-11-19 10:23:06 +01:00
|
|
|
website_email,
|
2020-02-10 17:23:40 +01:00
|
|
|
mailbox_email,
|
2019-11-19 10:23:06 +01:00
|
|
|
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,
|
2020-02-10 17:23:40 +01:00
|
|
|
mailbox_email,
|
2019-12-07 23:25:55 +01:00
|
|
|
msg_raw,
|
|
|
|
envelope.mail_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"
|
|
|
|
|
2020-01-04 10:25:19 +01:00
|
|
|
def handle_reply(self, envelope, smtp: SMTP, msg: Message) -> str:
|
|
|
|
reply_email = envelope.rcpt_tos[0].lower()
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2019-11-30 20:34:52 +01:00
|
|
|
# reply_email must end with EMAIL_DOMAIN
|
|
|
|
if not reply_email.endswith(EMAIL_DOMAIN):
|
2020-02-02 12:53:26 +01:00
|
|
|
LOG.warning(f"Reply email {reply_email} has wrong domain")
|
2019-11-30 20:34:52 +01:00
|
|
|
return "550 wrong reply email"
|
|
|
|
|
2019-11-19 10:23:06 +01:00
|
|
|
forward_email = ForwardEmail.get_by(reply_email=reply_email)
|
2020-02-02 12:53:26 +01:00
|
|
|
if not forward_email:
|
|
|
|
LOG.warning(f"No such forward-email with {reply_email} as reply-email")
|
|
|
|
return "550 wrong reply email"
|
|
|
|
|
2019-12-06 21:06:38 +01:00
|
|
|
alias: str = forward_email.gen_email.email
|
2019-12-25 18:23:43 +01:00
|
|
|
alias_domain = alias[alias.find("@") + 1 :]
|
2020-01-22 10:22:59 +01:00
|
|
|
|
|
|
|
# alias must end with one of the ALIAS_DOMAINS or custom-domain
|
|
|
|
if not email_belongs_to_alias_domains(alias):
|
2019-12-25 18:23:43 +01:00
|
|
|
if not CustomDomain.get_by(domain=alias_domain):
|
|
|
|
return "550 alias unknown by SimpleLogin"
|
|
|
|
|
2020-02-10 17:24:14 +01:00
|
|
|
gen_email = forward_email.gen_email
|
|
|
|
if gen_email.mailbox_id:
|
|
|
|
mailbox_email = gen_email.mailbox.email
|
|
|
|
else:
|
|
|
|
mailbox_email = gen_email.user.email
|
|
|
|
|
|
|
|
if envelope.mail_from.lower() != mailbox_email.lower():
|
2020-01-31 06:06:34 +01:00
|
|
|
LOG.warning(
|
2020-01-22 23:21:36 +01:00
|
|
|
f"Reply email can only be used by user email. Actual mail_from: %s. msg from header: %s, User email %s. reply_email %s",
|
2019-12-15 15:50:04 +01:00
|
|
|
envelope.mail_from,
|
2020-01-22 23:21:36 +01:00
|
|
|
msg["From"],
|
2020-02-10 17:24:14 +01:00
|
|
|
mailbox_email,
|
2020-01-10 18:23:37 +01:00
|
|
|
reply_email,
|
2019-12-15 15:50:04 +01:00
|
|
|
)
|
2019-12-15 17:04:46 +01:00
|
|
|
|
2020-02-11 16:46:53 +01:00
|
|
|
user = gen_email.user
|
|
|
|
send_email(
|
|
|
|
mailbox_email,
|
|
|
|
f"Reply from your alias {alias} only works from your mailbox",
|
|
|
|
render(
|
|
|
|
"transactional/reply-must-use-personal-email.txt",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
|
|
|
sender=envelope.mail_from,
|
|
|
|
mailbox_email=mailbox_email,
|
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/reply-must-use-personal-email.html",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
|
|
|
sender=envelope.mail_from,
|
|
|
|
mailbox_email=mailbox_email,
|
|
|
|
),
|
2020-01-22 23:18:27 +01:00
|
|
|
)
|
|
|
|
|
2020-02-11 16:58:30 +01:00
|
|
|
# Notify sender that they cannot send emails to this address
|
2019-12-16 18:36:59 +01:00
|
|
|
send_email(
|
2019-12-15 17:04:46 +01:00
|
|
|
envelope.mail_from,
|
2020-02-11 16:58:30 +01:00
|
|
|
f"Your email ({envelope.mail_from}) is not allowed to send emails to {reply_email}",
|
|
|
|
render(
|
|
|
|
"transactional/send-from-alias-from-unknown-sender.txt",
|
|
|
|
sender=envelope.mail_from,
|
|
|
|
reply_email=reply_email,
|
|
|
|
),
|
2019-12-15 17:27:07 +01:00
|
|
|
"",
|
2019-12-15 17:04:46 +01:00
|
|
|
)
|
|
|
|
|
2020-01-10 11:26:51 +01:00
|
|
|
return "550 ignored"
|
2019-12-15 15:50:04 +01:00
|
|
|
|
2020-01-07 19:49:26 +01:00
|
|
|
delete_header(msg, "DKIM-Signature")
|
2019-12-17 17:48:06 +01:00
|
|
|
|
2020-01-07 19:14:36 +01:00
|
|
|
# the email comes from alias
|
2019-11-19 10:23:06 +01:00
|
|
|
msg.replace_header("From", alias)
|
2020-01-07 19:14:36 +01:00
|
|
|
|
|
|
|
# some email providers like ProtonMail adds automatically the Reply-To field
|
2020-01-07 19:50:24 +01:00
|
|
|
# make sure to delete it
|
|
|
|
delete_header(msg, "Reply-To")
|
2020-01-07 19:14:36 +01:00
|
|
|
|
2019-11-19 10:23:06 +01:00
|
|
|
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
|
|
|
|
2020-01-08 12:44:29 +01:00
|
|
|
# Received-SPF is injected by postfix-policyd-spf-python can reveal user original email
|
|
|
|
delete_header(msg, "Received-SPF")
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2020-01-22 10:22:59 +01:00
|
|
|
if alias_domain in ALIAS_DOMAINS:
|
|
|
|
add_dkim_signature(msg, alias_domain)
|
|
|
|
# add DKIM-Signature for custom-domain alias
|
2019-12-25 18:23:43 +01:00
|
|
|
else:
|
|
|
|
custom_domain: CustomDomain = CustomDomain.get_by(domain=alias_domain)
|
|
|
|
if custom_domain.dkim_verified:
|
2019-12-25 18:33:24 +01:00
|
|
|
add_dkim_signature(msg, alias_domain)
|
2019-12-17 20:43:31 +01:00
|
|
|
|
2019-12-12 21:11:01 +01:00
|
|
|
msg_raw = msg.as_string().encode()
|
|
|
|
smtp.sendmail(
|
|
|
|
alias,
|
|
|
|
forward_email.website_email,
|
|
|
|
msg_raw,
|
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
2019-11-19 10:23:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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-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)
|