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:
|
2020-03-26 11:19:20 +01:00
|
|
|
- contact: who sends emails to alias@sl.co address
|
2019-11-07 17:49:26 +01:00
|
|
|
- SL email handler (this script)
|
2020-03-26 11:19:20 +01:00
|
|
|
- user personal email: to be protected. Should never leak to contact.
|
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:
|
2020-03-26 11:19:20 +01:00
|
|
|
mail from: @contact
|
2019-11-08 11:05:34 +01:00
|
|
|
rcpt to: @personal_email
|
2019-11-07 17:49:26 +01:00
|
|
|
Header:
|
2020-03-26 11:19:20 +01:00
|
|
|
From: @contact
|
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:
|
2020-03-26 11:19:20 +01:00
|
|
|
mail from: @contact
|
|
|
|
rcpt to: @contact
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
Header:
|
2020-03-26 11:19:20 +01:00
|
|
|
From: alias@sl.co # so for contact the email comes from alias. magic HERE
|
|
|
|
To: @contact
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
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
|
2020-03-26 11:19:20 +01:00
|
|
|
- @contact
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
2020-05-25 16:08:10 +02:00
|
|
|
import arrow
|
2020-04-02 18:10:08 +02:00
|
|
|
import email
|
2020-05-25 16:08:10 +02:00
|
|
|
import spf
|
2019-11-07 17:49:26 +01:00
|
|
|
import time
|
2020-03-15 22:29:53 +01:00
|
|
|
import uuid
|
2020-05-25 16:08:10 +02:00
|
|
|
from aiosmtpd.controller import Controller
|
|
|
|
from aiosmtpd.smtp import Envelope
|
2020-03-08 23:07:23 +01:00
|
|
|
from email import encoders
|
2020-01-04 10:25:19 +01:00
|
|
|
from email.message import Message
|
2020-03-08 23:07:23 +01:00
|
|
|
from email.mime.application import MIMEApplication
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
2020-04-05 15:27:35 +02:00
|
|
|
from email.utils import parseaddr, formataddr
|
2020-03-14 16:34:23 +01:00
|
|
|
from io import BytesIO
|
2019-11-07 17:49:26 +01:00
|
|
|
from smtplib import SMTP
|
2020-05-10 16:57:47 +02:00
|
|
|
from typing import List, Tuple
|
2019-11-07 17:49:26 +01:00
|
|
|
|
2020-03-14 16:34:23 +01:00
|
|
|
from app import pgp_utils, s3
|
2020-04-04 15:24:27 +02:00
|
|
|
from app.alias_utils import try_auto_create
|
2020-02-15 11:04:22 +01:00
|
|
|
from app.config import (
|
|
|
|
EMAIL_DOMAIN,
|
|
|
|
POSTFIX_SERVER,
|
|
|
|
URL,
|
|
|
|
ALIAS_DOMAINS,
|
2020-03-03 10:48:55 +01:00
|
|
|
POSTFIX_SUBMISSION_TLS,
|
2020-03-28 23:19:25 +01:00
|
|
|
UNSUBSCRIBER,
|
2020-04-14 12:45:47 +02:00
|
|
|
LOAD_PGP_EMAIL_HANDLER,
|
2020-05-07 13:28:04 +02:00
|
|
|
ENFORCE_SPF,
|
2020-05-09 20:45:04 +02:00
|
|
|
ALERT_REVERSE_ALIAS_UNKNOWN_MAILBOX,
|
|
|
|
ALERT_BOUNCE_EMAIL,
|
|
|
|
ALERT_SPAM_EMAIL,
|
2020-05-09 23:00:30 +02:00
|
|
|
ALERT_SPF,
|
2020-05-21 20:43:03 +02:00
|
|
|
POSTFIX_PORT,
|
2020-02-15 11:04:22 +01:00
|
|
|
)
|
2019-12-17 17:48:06 +01:00
|
|
|
from app.email_utils import (
|
|
|
|
send_email,
|
|
|
|
add_dkim_signature,
|
2020-01-07 19:50:36 +01:00
|
|
|
add_or_replace_header,
|
|
|
|
delete_header,
|
2020-01-22 10:22:59 +01:00
|
|
|
email_belongs_to_alias_domains,
|
2020-02-11 16:46:53 +01:00
|
|
|
render,
|
2020-03-14 16:34:23 +01:00
|
|
|
get_orig_message_from_bounce,
|
2020-03-14 22:24:02 +01:00
|
|
|
delete_all_headers_except,
|
2020-03-28 19:16:55 +01:00
|
|
|
get_addrs_from_header,
|
2020-03-30 22:05:31 +02:00
|
|
|
get_spam_info,
|
|
|
|
get_orig_message_from_spamassassin_report,
|
2020-04-05 14:50:12 +02:00
|
|
|
parseaddr_unicode,
|
2020-05-09 20:45:04 +02:00
|
|
|
send_email_with_rate_control,
|
2020-01-07 19:50:36 +01:00
|
|
|
)
|
2019-11-07 17:49:26 +01:00
|
|
|
from app.extensions import db
|
2020-04-04 16:27:22 +02:00
|
|
|
from app.greylisting import greylisting_needed
|
2019-11-07 17:49:26 +01:00
|
|
|
from app.log import LOG
|
2020-01-30 08:43:31 +01:00
|
|
|
from app.models import (
|
2020-03-17 11:51:40 +01:00
|
|
|
Alias,
|
2020-03-17 10:56:59 +01:00
|
|
|
Contact,
|
2020-03-17 11:10:50 +01:00
|
|
|
EmailLog,
|
2020-01-30 08:43:31 +01:00
|
|
|
CustomDomain,
|
|
|
|
User,
|
2020-03-14 16:34:23 +01:00
|
|
|
RefusedEmail,
|
2020-05-07 13:28:04 +02:00
|
|
|
Mailbox,
|
2020-01-30 08:43:31 +01:00
|
|
|
)
|
2019-12-15 17:04:46 +01:00
|
|
|
from app.utils import random_string
|
2020-04-14 12:45:47 +02:00
|
|
|
from init_app import load_pgp_public_keys
|
2019-11-08 09:11:01 +01:00
|
|
|
from server import create_app
|
2019-11-07 17:49:26 +01:00
|
|
|
|
2020-05-09 17:30:21 +02:00
|
|
|
_IP_HEADER = "X-SimpleLogin-Client-IP"
|
2020-05-10 17:52:07 +02:00
|
|
|
_MAILBOX_ID_HEADER = "X-SimpleLogin-Mailbox-ID"
|
2020-04-13 20:51:29 +02: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
|
|
|
|
|
|
|
|
|
2020-05-13 22:35:27 +02:00
|
|
|
def get_or_create_contact(
|
|
|
|
contact_from_header: str, mail_from: str, alias: Alias
|
|
|
|
) -> Contact:
|
2020-02-19 16:49:40 +01:00
|
|
|
"""
|
2020-04-05 15:24:09 +02:00
|
|
|
contact_from_header is the RFC 2047 format FROM header
|
2020-02-19 16:49:40 +01:00
|
|
|
"""
|
2020-05-15 15:46:37 +02:00
|
|
|
# contact_from_header can be None, use mail_from in this case instead
|
|
|
|
contact_from_header = contact_from_header or mail_from
|
|
|
|
|
2020-04-05 15:42:09 +02:00
|
|
|
# force convert header to string, sometimes contact_from_header is Header object
|
|
|
|
contact_from_header = str(contact_from_header)
|
2020-05-15 15:46:37 +02:00
|
|
|
|
2020-04-05 15:24:09 +02:00
|
|
|
contact_name, contact_email = parseaddr_unicode(contact_from_header)
|
2020-05-13 22:35:27 +02:00
|
|
|
if not contact_email:
|
2020-05-15 15:46:37 +02:00
|
|
|
# From header is wrongly formatted, try with mail_from
|
2020-05-13 22:35:27 +02:00
|
|
|
LOG.warning("From header is empty, parse mail_from %s %s", mail_from, alias)
|
|
|
|
contact_name, contact_email = parseaddr_unicode(mail_from)
|
2020-05-15 15:46:37 +02:00
|
|
|
if not contact_email:
|
|
|
|
LOG.error(
|
|
|
|
"Cannot parse contact from from_header:%s, mail_from:%s",
|
|
|
|
contact_from_header,
|
|
|
|
mail_from,
|
|
|
|
)
|
2020-05-13 22:35:27 +02:00
|
|
|
|
2020-04-04 20:06:35 +02:00
|
|
|
contact = Contact.get_by(alias_id=alias.id, website_email=contact_email)
|
2020-03-17 10:56:59 +01:00
|
|
|
if contact:
|
2020-04-05 15:24:09 +02:00
|
|
|
if contact.name != contact_name:
|
|
|
|
LOG.d(
|
|
|
|
"Update contact %s name %s to %s", contact, contact.name, contact_name,
|
|
|
|
)
|
|
|
|
contact.name = contact_name
|
2020-02-19 16:17:13 +01:00
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
LOG.debug(
|
2020-04-05 15:24:09 +02:00
|
|
|
"create contact for alias %s and contact %s", alias, contact_from_header,
|
2020-02-19 16:17:13 +01:00
|
|
|
)
|
2020-02-19 15:50:38 +01:00
|
|
|
|
2020-03-22 23:52:59 +01:00
|
|
|
reply_email = generate_reply_email()
|
2020-02-19 16:17:13 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
contact = Contact.create(
|
2020-03-20 09:54:38 +01:00
|
|
|
user_id=alias.user_id,
|
2020-03-17 12:01:18 +01:00
|
|
|
alias_id=alias.id,
|
2020-04-04 20:06:35 +02:00
|
|
|
website_email=contact_email,
|
2020-04-05 15:24:09 +02:00
|
|
|
name=contact_name,
|
2020-02-19 16:17:13 +01:00
|
|
|
reply_email=reply_email,
|
|
|
|
)
|
|
|
|
db.session.commit()
|
2020-02-19 15:50:38 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
return contact
|
2020-02-19 16:49:40 +01:00
|
|
|
|
|
|
|
|
2020-03-28 19:16:55 +01:00
|
|
|
def replace_header_when_forward(msg: Message, alias: Alias, header: str):
|
|
|
|
"""
|
|
|
|
Replace CC or To header by Reply emails in forward phase
|
|
|
|
"""
|
|
|
|
addrs = get_addrs_from_header(msg, header)
|
|
|
|
|
|
|
|
# Nothing to do
|
|
|
|
if not addrs:
|
|
|
|
return
|
|
|
|
|
|
|
|
new_addrs: [str] = []
|
|
|
|
need_replace = False
|
|
|
|
|
|
|
|
for addr in addrs:
|
2020-04-05 14:50:12 +02:00
|
|
|
contact_name, contact_email = parseaddr_unicode(addr)
|
2020-03-28 19:16:55 +01:00
|
|
|
|
|
|
|
# no transformation when alias is already in the header
|
2020-04-05 12:59:36 +02:00
|
|
|
if contact_email == alias.email:
|
2020-03-28 19:16:55 +01:00
|
|
|
new_addrs.append(addr)
|
|
|
|
continue
|
|
|
|
|
2020-04-05 12:59:36 +02:00
|
|
|
contact = Contact.get_by(alias_id=alias.id, website_email=contact_email)
|
2020-03-28 19:16:55 +01:00
|
|
|
if contact:
|
2020-04-05 15:39:48 +02:00
|
|
|
# update the contact name if needed
|
2020-04-05 14:50:12 +02:00
|
|
|
if contact.name != contact_name:
|
|
|
|
LOG.d(
|
|
|
|
"Update contact %s name %s to %s",
|
|
|
|
contact,
|
|
|
|
contact.name,
|
|
|
|
contact_name,
|
|
|
|
)
|
|
|
|
contact.name = contact_name
|
2020-03-28 19:16:55 +01:00
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
LOG.debug(
|
|
|
|
"create contact for alias %s and email %s, header %s",
|
|
|
|
alias,
|
2020-04-05 12:59:36 +02:00
|
|
|
contact_email,
|
2020-03-28 19:16:55 +01:00
|
|
|
header,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply_email = generate_reply_email()
|
|
|
|
|
|
|
|
contact = Contact.create(
|
|
|
|
user_id=alias.user_id,
|
|
|
|
alias_id=alias.id,
|
2020-04-05 12:59:36 +02:00
|
|
|
website_email=contact_email,
|
2020-04-05 15:24:09 +02:00
|
|
|
name=contact_name,
|
2020-03-28 19:16:55 +01:00
|
|
|
reply_email=reply_email,
|
|
|
|
is_cc=header.lower() == "cc",
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-04-05 15:24:09 +02:00
|
|
|
new_addrs.append(contact.new_addr())
|
2020-03-28 19:16:55 +01:00
|
|
|
need_replace = True
|
|
|
|
|
|
|
|
if need_replace:
|
|
|
|
new_header = ",".join(new_addrs)
|
|
|
|
LOG.d("Replace %s header, old: %s, new: %s", header, msg[header], new_header)
|
|
|
|
add_or_replace_header(msg, header, new_header)
|
|
|
|
else:
|
|
|
|
LOG.d("No need to replace %s header", header)
|
|
|
|
|
|
|
|
|
|
|
|
def replace_header_when_reply(msg: Message, alias: Alias, header: str):
|
|
|
|
"""
|
|
|
|
Replace CC or To Reply emails by original emails
|
|
|
|
"""
|
|
|
|
addrs = get_addrs_from_header(msg, header)
|
|
|
|
|
|
|
|
# Nothing to do
|
|
|
|
if not addrs:
|
|
|
|
return
|
|
|
|
|
|
|
|
new_addrs: [str] = []
|
|
|
|
|
|
|
|
for addr in addrs:
|
2020-04-05 15:27:35 +02:00
|
|
|
name, reply_email = parseaddr(addr)
|
2020-03-28 19:16:55 +01:00
|
|
|
|
|
|
|
# no transformation when alias is already in the header
|
2020-04-05 15:27:35 +02:00
|
|
|
if reply_email == alias.email:
|
2020-03-28 19:16:55 +01:00
|
|
|
continue
|
|
|
|
|
2020-04-05 15:27:35 +02:00
|
|
|
contact = Contact.get_by(reply_email=reply_email)
|
2020-03-28 19:16:55 +01:00
|
|
|
if not contact:
|
2020-03-30 21:45:18 +02:00
|
|
|
LOG.warning(
|
2020-04-05 15:27:35 +02:00
|
|
|
"%s email in reply phase %s must be reply emails", header, reply_email
|
2020-03-30 21:45:18 +02:00
|
|
|
)
|
2020-03-28 19:16:55 +01:00
|
|
|
# still keep this email in header
|
|
|
|
new_addrs.append(addr)
|
2020-04-05 15:27:35 +02:00
|
|
|
else:
|
|
|
|
new_addrs.append(formataddr((contact.name, contact.website_email)))
|
2020-03-28 19:16:55 +01:00
|
|
|
|
2020-03-30 21:45:18 +02:00
|
|
|
new_header = ",".join(new_addrs)
|
|
|
|
LOG.d("Replace %s header, old: %s, new: %s", header, msg[header], new_header)
|
|
|
|
add_or_replace_header(msg, header, new_header)
|
2020-03-28 19:16:55 +01:00
|
|
|
|
|
|
|
|
2020-05-25 16:08:10 +02:00
|
|
|
def replace_str_in_msg(msg: Message, fr: str, to: str):
|
|
|
|
if msg.get_content_maintype() != "text":
|
|
|
|
return msg
|
|
|
|
new_body = msg.get_payload(decode=True).replace(fr.encode(), to.encode())
|
|
|
|
|
|
|
|
# If utf-8 decoding fails, do not touch message part
|
|
|
|
try:
|
|
|
|
new_body = new_body.decode("utf-8")
|
|
|
|
except:
|
|
|
|
return msg
|
|
|
|
|
|
|
|
cte = (
|
|
|
|
msg["Content-Transfer-Encoding"].lower()
|
|
|
|
if msg["Content-Transfer-Encoding"]
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
subtype = msg.get_content_subtype()
|
|
|
|
delete_header(msg, "Content-Transfer-Encoding")
|
|
|
|
delete_header(msg, "Content-Type")
|
|
|
|
|
|
|
|
email.contentmanager.set_text_content(msg, new_body, subtype=subtype, cte=cte)
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
2020-03-22 23:52:59 +01:00
|
|
|
def generate_reply_email():
|
|
|
|
# generate a reply_email, make sure it is unique
|
|
|
|
# not use while loop to avoid infinite loop
|
|
|
|
reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}"
|
|
|
|
for _ in range(1000):
|
|
|
|
if not Contact.get_by(reply_email=reply_email):
|
|
|
|
# found!
|
|
|
|
break
|
|
|
|
reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}"
|
|
|
|
|
|
|
|
return reply_email
|
|
|
|
|
|
|
|
|
2020-03-17 12:10:13 +01:00
|
|
|
def should_append_alias(msg: Message, address: str):
|
2020-03-28 19:16:55 +01:00
|
|
|
"""whether an alias should be appended to TO header in message"""
|
2020-03-05 21:13:36 +01:00
|
|
|
|
2020-05-20 22:35:28 +02:00
|
|
|
if msg["To"] and address.lower() in msg["To"].lower():
|
2020-03-05 21:13:36 +01:00
|
|
|
return False
|
2020-05-20 22:35:28 +02:00
|
|
|
if msg["Cc"] and address.lower() in msg["Cc"].lower():
|
2020-03-05 21:13:36 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-04-14 20:49:48 +02:00
|
|
|
_MIME_HEADERS = [
|
|
|
|
"MIME-Version",
|
|
|
|
"Content-Type",
|
|
|
|
"Content-Disposition",
|
|
|
|
"Content-Transfer-Encoding",
|
|
|
|
]
|
|
|
|
_MIME_HEADERS = [h.lower() for h in _MIME_HEADERS]
|
|
|
|
|
|
|
|
|
2020-03-08 23:07:23 +01:00
|
|
|
def prepare_pgp_message(orig_msg: Message, pgp_fingerprint: str):
|
|
|
|
msg = MIMEMultipart("encrypted", protocol="application/pgp-encrypted")
|
|
|
|
|
2020-04-14 20:49:48 +02:00
|
|
|
# copy all headers from original message except all standard MIME headers
|
2020-03-08 23:07:23 +01:00
|
|
|
for i in reversed(range(len(orig_msg._headers))):
|
|
|
|
header_name = orig_msg._headers[i][0].lower()
|
2020-04-14 20:49:48 +02:00
|
|
|
if header_name.lower() not in _MIME_HEADERS:
|
2020-03-08 23:07:23 +01:00
|
|
|
msg[header_name] = orig_msg._headers[i][1]
|
|
|
|
|
2020-03-14 22:24:02 +01:00
|
|
|
# Delete unnecessary headers in orig_msg except to save space
|
|
|
|
delete_all_headers_except(
|
2020-04-14 20:49:48 +02:00
|
|
|
orig_msg, _MIME_HEADERS,
|
2020-03-14 22:24:02 +01:00
|
|
|
)
|
|
|
|
|
2020-03-08 23:07:23 +01:00
|
|
|
first = MIMEApplication(
|
|
|
|
_subtype="pgp-encrypted", _encoder=encoders.encode_7or8bit, _data=""
|
|
|
|
)
|
|
|
|
first.set_payload("Version: 1")
|
|
|
|
msg.attach(first)
|
|
|
|
|
|
|
|
second = MIMEApplication("octet-stream", _encoder=encoders.encode_7or8bit)
|
|
|
|
second.add_header("Content-Disposition", "inline")
|
|
|
|
# encrypt original message
|
2020-04-02 21:29:16 +02:00
|
|
|
encrypted_data = pgp_utils.encrypt_file(
|
|
|
|
BytesIO(orig_msg.as_bytes()), pgp_fingerprint
|
|
|
|
)
|
2020-03-08 23:07:23 +01:00
|
|
|
second.set_payload(encrypted_data)
|
|
|
|
msg.attach(second)
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
2020-05-10 16:57:47 +02:00
|
|
|
def handle_forward(
|
|
|
|
envelope, smtp: SMTP, msg: Message, rcpt_to: str
|
|
|
|
) -> List[Tuple[bool, str]]:
|
2020-03-28 21:24:43 +01:00
|
|
|
"""return whether an email has been delivered and
|
|
|
|
the smtp status ("250 Message accepted", "550 Non-existent email address", etc)
|
|
|
|
"""
|
2020-05-10 16:33:54 +02:00
|
|
|
address = rcpt_to.lower().strip() # alias@SL
|
2020-02-19 16:49:40 +01:00
|
|
|
|
2020-03-19 11:15:02 +01:00
|
|
|
alias = Alias.get_by(email=address)
|
2020-03-17 11:51:40 +01:00
|
|
|
if not alias:
|
2020-03-28 11:04:58 +01:00
|
|
|
LOG.d("alias %s not exist. Try to see if it can be created on the fly", address)
|
2020-03-19 11:15:02 +01:00
|
|
|
alias = try_auto_create(address)
|
2020-03-17 11:51:40 +01:00
|
|
|
if not alias:
|
2020-03-28 11:04:58 +01:00
|
|
|
LOG.d("alias %s cannot be created on-the-fly, return 550", address)
|
2020-05-10 16:57:47 +02:00
|
|
|
return [(False, "550 SL E3")]
|
2020-02-19 16:49:40 +01:00
|
|
|
|
2020-05-13 22:35:27 +02:00
|
|
|
contact = get_or_create_contact(msg["From"], envelope.mail_from, alias)
|
2020-04-27 18:18:40 +02:00
|
|
|
email_log = EmailLog.create(contact_id=contact.id, user_id=contact.user_id)
|
|
|
|
|
|
|
|
if not alias.enabled:
|
|
|
|
LOG.d("%s is disabled, do not forward", alias)
|
|
|
|
email_log.blocked = True
|
|
|
|
|
|
|
|
db.session.commit()
|
2020-05-10 16:31:36 +02:00
|
|
|
# do not return 5** to allow user to receive emails later when alias is enabled
|
2020-05-10 16:57:47 +02:00
|
|
|
return [(True, "250 Message accepted for delivery")]
|
2020-03-30 22:05:31 +02:00
|
|
|
|
2020-05-10 16:31:36 +02:00
|
|
|
user = alias.user
|
|
|
|
|
2020-05-10 16:57:47 +02:00
|
|
|
ret = []
|
|
|
|
for mailbox in alias.mailboxes:
|
|
|
|
ret.append(
|
|
|
|
forward_email_to_mailbox(
|
|
|
|
alias, msg, email_log, contact, envelope, smtp, mailbox, user
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def forward_email_to_mailbox(
|
|
|
|
alias,
|
|
|
|
msg: Message,
|
|
|
|
email_log: EmailLog,
|
|
|
|
contact: Contact,
|
|
|
|
envelope,
|
|
|
|
smtp: SMTP,
|
|
|
|
mailbox,
|
|
|
|
user,
|
|
|
|
) -> (bool, str):
|
|
|
|
LOG.d("Forward %s -> %s -> %s", contact, alias, mailbox)
|
2020-05-15 16:34:07 +02:00
|
|
|
is_spam, spam_status = get_spam_info(msg)
|
|
|
|
if is_spam:
|
|
|
|
LOG.warning("Email detected as spam. Alias: %s, from: %s", alias, contact)
|
|
|
|
email_log.is_spam = True
|
|
|
|
email_log.spam_status = spam_status
|
|
|
|
|
2020-05-16 11:28:25 +02:00
|
|
|
handle_spam(contact, alias, msg, user, mailbox.email, email_log)
|
2020-05-15 16:34:07 +02:00
|
|
|
return False, "550 SL E1"
|
2020-03-30 22:05:31 +02:00
|
|
|
|
2020-03-08 23:07:23 +01:00
|
|
|
# create PGP email if needed
|
2020-05-16 18:20:26 +02:00
|
|
|
if mailbox.pgp_finger_print and user.is_premium() and not alias.disable_pgp:
|
2020-03-08 23:07:23 +01:00
|
|
|
LOG.d("Encrypt message using mailbox %s", mailbox)
|
|
|
|
msg = prepare_pgp_message(msg, mailbox.pgp_finger_print)
|
|
|
|
|
2020-04-27 18:18:40 +02:00
|
|
|
# add custom header
|
|
|
|
add_or_replace_header(msg, "X-SimpleLogin-Type", "Forward")
|
|
|
|
|
|
|
|
# remove reply-to & sender header if present
|
|
|
|
delete_header(msg, "Reply-To")
|
|
|
|
delete_header(msg, "Sender")
|
|
|
|
|
2020-05-09 17:30:21 +02:00
|
|
|
delete_header(msg, _IP_HEADER)
|
2020-05-10 17:52:07 +02:00
|
|
|
add_or_replace_header(msg, _MAILBOX_ID_HEADER, str(mailbox.id))
|
2020-05-09 17:30:21 +02:00
|
|
|
|
2020-04-27 18:18:40 +02:00
|
|
|
# change the from header so the sender comes from @SL
|
|
|
|
# so it can pass DMARC check
|
|
|
|
# replace the email part in from: header
|
|
|
|
contact_from_header = msg["From"]
|
|
|
|
new_from_header = contact.new_addr()
|
|
|
|
add_or_replace_header(msg, "From", new_from_header)
|
|
|
|
LOG.d("new_from_header:%s, old header %s", new_from_header, contact_from_header)
|
|
|
|
|
|
|
|
# replace CC & To emails by reply-email for all emails that are not alias
|
|
|
|
replace_header_when_forward(msg, alias, "Cc")
|
|
|
|
replace_header_when_forward(msg, alias, "To")
|
|
|
|
|
|
|
|
# append alias into the TO header if it's not present in To or CC
|
|
|
|
if should_append_alias(msg, alias.email):
|
|
|
|
LOG.d("append alias %s to TO header %s", alias, msg["To"])
|
|
|
|
if msg["To"]:
|
|
|
|
to_header = msg["To"] + "," + alias.email
|
2020-03-28 23:19:25 +01:00
|
|
|
else:
|
2020-04-27 18:18:40 +02:00
|
|
|
to_header = alias.email
|
2020-02-19 16:17:13 +01:00
|
|
|
|
2020-04-27 18:18:40 +02:00
|
|
|
add_or_replace_header(msg, "To", to_header.strip())
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-04-27 18:18:40 +02:00
|
|
|
# add List-Unsubscribe header
|
|
|
|
if UNSUBSCRIBER:
|
|
|
|
unsubscribe_link = f"mailto:{UNSUBSCRIBER}?subject={alias.id}="
|
|
|
|
add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
|
|
|
|
else:
|
|
|
|
unsubscribe_link = f"{URL}/dashboard/unsubscribe/{alias.id}"
|
|
|
|
add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
|
|
|
|
add_or_replace_header(
|
|
|
|
msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
|
2019-11-19 10:23:06 +01:00
|
|
|
)
|
2019-12-15 10:18:33 +01:00
|
|
|
|
2020-04-27 18:18:40 +02:00
|
|
|
add_dkim_signature(msg, EMAIL_DOMAIN)
|
|
|
|
|
|
|
|
LOG.d(
|
|
|
|
"Forward mail from %s to %s, mail_options %s, rcpt_options %s ",
|
|
|
|
contact.website_email,
|
2020-05-10 16:32:54 +02:00
|
|
|
mailbox.email,
|
2020-04-27 18:18:40 +02:00
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
|
|
|
)
|
|
|
|
|
|
|
|
# smtp.send_message has UnicodeEncodeErroremail issue
|
|
|
|
# encode message raw directly instead
|
|
|
|
smtp.sendmail(
|
|
|
|
contact.reply_email,
|
2020-05-10 16:32:54 +02:00
|
|
|
mailbox.email,
|
2020-04-27 18:18:40 +02:00
|
|
|
msg.as_bytes(),
|
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
|
|
|
)
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
db.session.commit()
|
2020-03-28 21:24:43 +01:00
|
|
|
return True, "250 Message accepted for delivery"
|
2019-11-19 10:23:06 +01:00
|
|
|
|
|
|
|
|
2020-03-28 21:24:43 +01:00
|
|
|
def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> (bool, str):
|
|
|
|
"""
|
|
|
|
return whether an email has been delivered and
|
|
|
|
the smtp status ("250 Message accepted", "550 Non-existent email address", etc)
|
|
|
|
"""
|
2020-05-20 22:34:06 +02:00
|
|
|
reply_email = rcpt_to.lower().strip()
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
# reply_email must end with EMAIL_DOMAIN
|
|
|
|
if not reply_email.endswith(EMAIL_DOMAIN):
|
|
|
|
LOG.warning(f"Reply email {reply_email} has wrong domain")
|
2020-04-13 19:33:45 +02:00
|
|
|
return False, "550 SL E2"
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
contact = Contact.get_by(reply_email=reply_email)
|
|
|
|
if not contact:
|
2020-02-19 16:17:13 +01:00
|
|
|
LOG.warning(f"No such forward-email with {reply_email} as reply-email")
|
2020-04-13 19:33:45 +02:00
|
|
|
return False, "550 SL E4"
|
2019-12-18 17:07:20 +01:00
|
|
|
|
2020-03-28 19:16:55 +01:00
|
|
|
alias = contact.alias
|
2020-03-17 11:51:40 +01:00
|
|
|
address: str = contact.alias.email
|
|
|
|
alias_domain = address[address.find("@") + 1 :]
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
# alias must end with one of the ALIAS_DOMAINS or custom-domain
|
2020-03-28 19:16:55 +01:00
|
|
|
if not email_belongs_to_alias_domains(alias.email):
|
2020-02-19 16:17:13 +01:00
|
|
|
if not CustomDomain.get_by(domain=alias_domain):
|
2020-04-13 19:33:45 +02:00
|
|
|
return False, "550 SL E5"
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
user = alias.user
|
2020-05-10 18:19:29 +02:00
|
|
|
mail_from = envelope.mail_from.lower().strip()
|
2020-02-19 16:17:13 +01:00
|
|
|
|
|
|
|
# bounce email initiated by Postfix
|
|
|
|
# can happen in case emails cannot be delivered to user-email
|
|
|
|
# in this case Postfix will try to send a bounce report to original sender, which is
|
|
|
|
# the "reply email"
|
2020-05-10 18:19:29 +02:00
|
|
|
if mail_from == "<>":
|
2020-03-29 23:12:06 +02:00
|
|
|
LOG.warning(
|
2020-05-10 18:19:29 +02:00
|
|
|
"Bounce when sending to alias %s from %s, user %s", alias, contact, user,
|
2020-03-14 16:34:23 +01:00
|
|
|
)
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-05-10 17:53:37 +02:00
|
|
|
handle_bounce(contact, alias, msg, user)
|
2020-04-13 19:33:45 +02:00
|
|
|
return False, "550 SL E6"
|
2020-02-19 16:17:13 +01:00
|
|
|
|
2020-05-10 18:19:29 +02:00
|
|
|
mailbox = Mailbox.get_by(email=mail_from, user_id=user.id)
|
|
|
|
if not mailbox or mailbox not in alias.mailboxes:
|
|
|
|
# only mailbox can send email to the reply-email
|
|
|
|
handle_unknown_mailbox(envelope, msg, reply_email, user, alias)
|
|
|
|
return False, "550 SL E7"
|
|
|
|
|
2020-05-09 23:09:11 +02:00
|
|
|
if ENFORCE_SPF and mailbox.force_spf:
|
2020-05-09 22:54:55 +02:00
|
|
|
ip = msg[_IP_HEADER]
|
2020-05-10 10:37:56 +02:00
|
|
|
if not spf_pass(ip, envelope, mailbox, user, alias, contact.website_email, msg):
|
2020-05-11 10:21:44 +02:00
|
|
|
# cannot use 4** here as sender will retry. 5** because that generates bounce report
|
|
|
|
return True, "250 SL E11"
|
2020-05-07 13:28:04 +02:00
|
|
|
|
2020-05-09 17:31:37 +02:00
|
|
|
delete_header(msg, _IP_HEADER)
|
2020-05-07 13:28:04 +02:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
delete_header(msg, "DKIM-Signature")
|
2020-03-30 22:05:51 +02:00
|
|
|
delete_header(msg, "Received")
|
|
|
|
|
2020-03-28 19:16:55 +01:00
|
|
|
# make the email comes from alias
|
2020-05-03 16:05:34 +02:00
|
|
|
from_header = alias.email
|
|
|
|
# add alias name from alias
|
2020-04-26 10:41:24 +02:00
|
|
|
if alias.name:
|
|
|
|
LOG.d("Put alias name in from header")
|
|
|
|
from_header = formataddr((alias.name, alias.email))
|
2020-05-03 16:05:34 +02:00
|
|
|
elif alias.custom_domain:
|
|
|
|
LOG.d("Put domain default alias name in from header")
|
|
|
|
|
|
|
|
# add alias name from domain
|
|
|
|
if alias.custom_domain.name:
|
|
|
|
from_header = formataddr((alias.custom_domain.name, alias.email))
|
|
|
|
|
2020-04-26 10:41:24 +02:00
|
|
|
add_or_replace_header(msg, "From", from_header)
|
2020-02-10 17:24:14 +01:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
# some email providers like ProtonMail adds automatically the Reply-To field
|
|
|
|
# make sure to delete it
|
|
|
|
delete_header(msg, "Reply-To")
|
2019-12-15 17:04:46 +01:00
|
|
|
|
2020-03-13 10:34:02 +01:00
|
|
|
# remove sender header if present as this could reveal user real email
|
|
|
|
delete_header(msg, "Sender")
|
2020-04-01 20:33:27 +02:00
|
|
|
delete_header(msg, "X-Sender")
|
2020-03-13 10:34:02 +01:00
|
|
|
|
2020-03-28 19:16:55 +01:00
|
|
|
replace_header_when_reply(msg, alias, "To")
|
|
|
|
replace_header_when_reply(msg, alias, "Cc")
|
2019-12-15 17:04:46 +01:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
# Received-SPF is injected by postfix-policyd-spf-python can reveal user original email
|
|
|
|
delete_header(msg, "Received-SPF")
|
2019-12-15 15:50:04 +01:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
LOG.d(
|
|
|
|
"send email from %s to %s, mail_options:%s,rcpt_options:%s",
|
2020-03-28 19:16:55 +01:00
|
|
|
alias.email,
|
2020-03-17 10:56:59 +01:00
|
|
|
contact.website_email,
|
2020-02-19 16:17:13 +01:00
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
|
|
|
)
|
2019-12-17 17:48:06 +01:00
|
|
|
|
2020-05-25 16:08:10 +02:00
|
|
|
# replace "ra+string@simplelogin.co" by the contact email in the email body
|
|
|
|
# as this is usually included when replying
|
2020-05-20 10:53:19 +02:00
|
|
|
if user.replace_reverse_alias:
|
2020-05-25 16:08:10 +02:00
|
|
|
if msg.is_multipart():
|
|
|
|
for part in msg.walk():
|
|
|
|
if part.get_content_maintype() != "text":
|
|
|
|
continue
|
|
|
|
part = replace_str_in_msg(part, reply_email, contact.website_email)
|
|
|
|
|
|
|
|
else:
|
|
|
|
msg = replace_str_in_msg(msg, reply_email, contact.website_email)
|
2020-05-20 10:53:19 +02:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
if alias_domain in ALIAS_DOMAINS:
|
|
|
|
add_dkim_signature(msg, alias_domain)
|
|
|
|
# add DKIM-Signature for custom-domain alias
|
|
|
|
else:
|
|
|
|
custom_domain: CustomDomain = CustomDomain.get_by(domain=alias_domain)
|
|
|
|
if custom_domain.dkim_verified:
|
|
|
|
add_dkim_signature(msg, alias_domain)
|
2020-01-07 19:14:36 +01:00
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
smtp.sendmail(
|
2020-03-28 19:16:55 +01:00
|
|
|
alias.email,
|
2020-03-17 10:56:59 +01:00
|
|
|
contact.website_email,
|
2020-05-20 10:53:19 +02:00
|
|
|
msg.as_bytes(),
|
2020-02-19 16:17:13 +01:00
|
|
|
envelope.mail_options,
|
|
|
|
envelope.rcpt_options,
|
|
|
|
)
|
2020-01-07 19:14:36 +01:00
|
|
|
|
2020-03-20 09:55:52 +01:00
|
|
|
EmailLog.create(contact_id=contact.id, is_reply=True, user_id=contact.user_id)
|
2020-02-19 16:17:13 +01:00
|
|
|
db.session.commit()
|
2019-11-19 10:23:06 +01:00
|
|
|
|
2020-03-28 21:24:43 +01:00
|
|
|
return True, "250 Message accepted for delivery"
|
2019-11-20 18:52:49 +01:00
|
|
|
|
2020-01-08 12:44:29 +01:00
|
|
|
|
2020-05-09 23:09:11 +02:00
|
|
|
def spf_pass(
|
2020-05-10 10:37:56 +02:00
|
|
|
ip: str,
|
|
|
|
envelope,
|
|
|
|
mailbox: Mailbox,
|
|
|
|
user: User,
|
|
|
|
alias: Alias,
|
|
|
|
contact_email: str,
|
|
|
|
msg: Message,
|
2020-05-09 23:09:11 +02:00
|
|
|
) -> bool:
|
|
|
|
if ip:
|
|
|
|
LOG.d("Enforce SPF")
|
|
|
|
try:
|
|
|
|
r = spf.check2(i=ip, s=envelope.mail_from.lower(), h=None)
|
|
|
|
except Exception:
|
|
|
|
LOG.error("SPF error, mailbox %s, ip %s", mailbox.email, ip)
|
|
|
|
else:
|
|
|
|
# TODO: Handle temperr case (e.g. dns timeout)
|
|
|
|
# only an absolute pass, or no SPF policy at all is 'valid'
|
|
|
|
if r[0] not in ["pass", "none"]:
|
|
|
|
LOG.error(
|
|
|
|
"SPF fail for mailbox %s, reason %s, failed IP %s",
|
|
|
|
mailbox.email,
|
|
|
|
r[0],
|
|
|
|
ip,
|
|
|
|
)
|
|
|
|
send_email_with_rate_control(
|
|
|
|
user,
|
|
|
|
ALERT_SPF,
|
|
|
|
mailbox.email,
|
|
|
|
f"SimpleLogin Alert: attempt to send emails from your alias {alias.email} from unknown IP Address",
|
|
|
|
render(
|
|
|
|
"transactional/spf-fail.txt",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias.email,
|
|
|
|
ip=ip,
|
|
|
|
mailbox_url=URL + f"/dashboard/mailbox/{mailbox.id}#spf",
|
2020-05-10 10:37:56 +02:00
|
|
|
to_email=contact_email,
|
|
|
|
subject=msg["Subject"],
|
|
|
|
time=arrow.now(),
|
2020-05-09 23:09:11 +02:00
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/spf-fail.html",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias.email,
|
|
|
|
ip=ip,
|
|
|
|
mailbox_url=URL + f"/dashboard/mailbox/{mailbox.id}#spf",
|
2020-05-10 10:37:56 +02:00
|
|
|
to_email=contact_email,
|
|
|
|
subject=msg["Subject"],
|
|
|
|
time=arrow.now(),
|
2020-05-09 23:09:11 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
LOG.warning(
|
|
|
|
"Could not find %s header %s -> %s",
|
|
|
|
_IP_HEADER,
|
|
|
|
mailbox.email,
|
|
|
|
contact_email,
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-05-10 18:19:29 +02:00
|
|
|
def handle_unknown_mailbox(envelope, msg, reply_email: str, user: User, alias: Alias):
|
2020-05-09 23:12:30 +02:00
|
|
|
LOG.warning(
|
|
|
|
f"Reply email can only be used by mailbox. "
|
2020-05-10 18:19:29 +02:00
|
|
|
f"Actual mail_from: %s. msg from header: %s, reverse-alias %s, %s %s",
|
2020-05-09 23:12:30 +02:00
|
|
|
envelope.mail_from,
|
|
|
|
msg["From"],
|
|
|
|
reply_email,
|
2020-05-10 18:19:29 +02:00
|
|
|
alias,
|
|
|
|
user,
|
2020-05-09 23:12:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
send_email_with_rate_control(
|
|
|
|
user,
|
|
|
|
ALERT_REVERSE_ALIAS_UNKNOWN_MAILBOX,
|
2020-05-10 18:19:29 +02:00
|
|
|
user.email,
|
2020-05-09 23:12:30 +02:00
|
|
|
f"Reply from your alias {alias.email} only works from your mailbox",
|
|
|
|
render(
|
|
|
|
"transactional/reply-must-use-personal-email.txt",
|
|
|
|
name=user.name,
|
2020-05-10 18:19:29 +02:00
|
|
|
alias=alias,
|
2020-05-09 23:12:30 +02:00
|
|
|
sender=envelope.mail_from,
|
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/reply-must-use-personal-email.html",
|
|
|
|
name=user.name,
|
2020-05-10 18:19:29 +02:00
|
|
|
alias=alias,
|
2020-05-09 23:12:30 +02:00
|
|
|
sender=envelope.mail_from,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Notify sender that they cannot send emails to this address
|
|
|
|
send_email_with_rate_control(
|
|
|
|
user,
|
|
|
|
ALERT_REVERSE_ALIAS_UNKNOWN_MAILBOX,
|
|
|
|
envelope.mail_from,
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/send-from-alias-from-unknown-sender.html",
|
|
|
|
sender=envelope.mail_from,
|
|
|
|
reply_email=reply_email,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-10 17:53:37 +02:00
|
|
|
def handle_bounce(contact: Contact, alias: Alias, msg: Message, user: User):
|
2020-03-17 12:12:11 +01:00
|
|
|
address = alias.email
|
2020-04-04 19:21:31 +02:00
|
|
|
email_log: EmailLog = EmailLog.create(
|
2020-03-20 09:55:52 +01:00
|
|
|
contact_id=contact.id, bounced=True, user_id=contact.user_id
|
|
|
|
)
|
2020-02-22 14:57:19 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-17 11:10:50 +01:00
|
|
|
nb_bounced = EmailLog.filter_by(contact_id=contact.id, bounced=True).count()
|
2020-03-17 11:51:40 +01:00
|
|
|
disable_alias_link = f"{URL}/dashboard/unsubscribe/{alias.id}"
|
2020-02-22 14:57:19 +01:00
|
|
|
|
2020-05-16 18:24:51 +02:00
|
|
|
# <<< Store the bounced email >>>
|
2020-03-15 18:39:59 +01:00
|
|
|
# generate a name for the email
|
|
|
|
random_name = str(uuid.uuid4())
|
2020-03-14 16:34:23 +01:00
|
|
|
|
|
|
|
full_report_path = f"refused-emails/full-{random_name}.eml"
|
2020-03-15 18:39:59 +01:00
|
|
|
s3.upload_email_from_bytesio(full_report_path, BytesIO(msg.as_bytes()), random_name)
|
2020-03-14 16:34:23 +01:00
|
|
|
|
2020-05-17 14:01:55 +02:00
|
|
|
file_path = None
|
|
|
|
mailbox = alias.mailbox
|
2020-05-16 18:24:51 +02:00
|
|
|
orig_msg = get_orig_message_from_bounce(msg)
|
2020-05-10 17:53:37 +02:00
|
|
|
if not orig_msg:
|
2020-05-17 14:01:55 +02:00
|
|
|
# Some MTA does not return the original message in bounce message
|
|
|
|
# nothing we can do here
|
2020-05-21 20:05:07 +02:00
|
|
|
LOG.warning(
|
2020-05-16 18:24:51 +02:00
|
|
|
"Cannot parse original message from bounce message %s %s %s %s",
|
2020-05-10 17:53:37 +02:00
|
|
|
alias,
|
|
|
|
user,
|
|
|
|
contact,
|
2020-05-16 18:24:51 +02:00
|
|
|
full_report_path,
|
2020-03-22 16:56:08 +01:00
|
|
|
)
|
2020-05-17 14:01:55 +02:00
|
|
|
else:
|
|
|
|
file_path = f"refused-emails/{random_name}.eml"
|
|
|
|
s3.upload_email_from_bytesio(
|
|
|
|
file_path, BytesIO(orig_msg.as_bytes()), random_name
|
2020-05-10 17:53:37 +02:00
|
|
|
)
|
2020-05-17 14:01:55 +02:00
|
|
|
# <<< END Store the bounced email >>>
|
|
|
|
|
|
|
|
mailbox_id = int(orig_msg[_MAILBOX_ID_HEADER])
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if not mailbox or mailbox.user_id != user.id:
|
|
|
|
LOG.error(
|
|
|
|
"Tampered message mailbox_id %s, %s, %s, %s %s",
|
|
|
|
mailbox_id,
|
|
|
|
user,
|
|
|
|
alias,
|
|
|
|
contact,
|
|
|
|
full_report_path,
|
|
|
|
)
|
|
|
|
# use the alias default mailbox
|
|
|
|
mailbox = alias.mailbox
|
2020-03-14 16:34:23 +01:00
|
|
|
|
|
|
|
refused_email = RefusedEmail.create(
|
|
|
|
path=file_path, full_report_path=full_report_path, user_id=user.id
|
|
|
|
)
|
|
|
|
db.session.flush()
|
|
|
|
|
2020-04-04 19:21:31 +02:00
|
|
|
email_log.refused_email_id = refused_email.id
|
2020-05-10 18:35:13 +02:00
|
|
|
email_log.bounced_mailbox_id = mailbox.id
|
2020-03-14 16:34:23 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
LOG.d("Create refused email %s", refused_email)
|
|
|
|
|
2020-04-04 19:21:31 +02:00
|
|
|
refused_email_url = (
|
|
|
|
URL + f"/dashboard/refused_email?highlight_id=" + str(email_log.id)
|
|
|
|
)
|
2020-03-14 16:34:23 +01:00
|
|
|
|
2020-02-22 14:57:19 +01:00
|
|
|
# inform user if this is the first bounced email
|
|
|
|
if nb_bounced == 1:
|
|
|
|
LOG.d(
|
|
|
|
"Inform user %s about bounced email sent by %s to alias %s",
|
|
|
|
user,
|
2020-04-05 11:59:24 +02:00
|
|
|
contact.website_email,
|
2020-03-17 11:51:40 +01:00
|
|
|
address,
|
2020-02-22 14:57:19 +01:00
|
|
|
)
|
2020-05-09 20:45:04 +02:00
|
|
|
send_email_with_rate_control(
|
|
|
|
user,
|
|
|
|
ALERT_BOUNCE_EMAIL,
|
2020-03-15 12:15:11 +01:00
|
|
|
# use user mail here as only user is authenticated to see the refused email
|
|
|
|
user.email,
|
2020-04-05 11:59:24 +02:00
|
|
|
f"Email from {contact.website_email} to {address} cannot be delivered to your inbox",
|
2020-02-22 14:57:19 +01:00
|
|
|
render(
|
|
|
|
"transactional/bounced-email.txt",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
2020-03-17 10:56:59 +01:00
|
|
|
website_email=contact.website_email,
|
2020-02-22 14:57:19 +01:00
|
|
|
disable_alias_link=disable_alias_link,
|
2020-03-14 16:34:23 +01:00
|
|
|
refused_email_url=refused_email_url,
|
2020-05-10 17:53:37 +02:00
|
|
|
mailbox_email=mailbox.email,
|
2020-02-22 14:57:19 +01:00
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/bounced-email.html",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
2020-03-17 10:56:59 +01:00
|
|
|
website_email=contact.website_email,
|
2020-02-22 14:57:19 +01:00
|
|
|
disable_alias_link=disable_alias_link,
|
2020-03-14 16:34:23 +01:00
|
|
|
refused_email_url=refused_email_url,
|
2020-05-10 17:53:37 +02:00
|
|
|
mailbox_email=mailbox.email,
|
2020-02-22 14:57:19 +01:00
|
|
|
),
|
2020-03-14 16:34:23 +01:00
|
|
|
# cannot include bounce email as it can contain spammy text
|
|
|
|
# bounced_email=msg,
|
2020-02-22 14:57:19 +01:00
|
|
|
)
|
|
|
|
# disable the alias the second time email is bounced
|
|
|
|
elif nb_bounced >= 2:
|
|
|
|
LOG.d(
|
|
|
|
"Bounce happens again with alias %s from %s. Disable alias now ",
|
2020-03-17 11:51:40 +01:00
|
|
|
address,
|
2020-04-05 11:59:24 +02:00
|
|
|
contact.website_email,
|
2020-02-22 14:57:19 +01:00
|
|
|
)
|
2020-05-27 14:12:32 +02:00
|
|
|
if alias.cannot_be_disabled:
|
|
|
|
LOG.warning("%s cannot be disabled", alias)
|
|
|
|
else:
|
|
|
|
alias.enabled = False
|
2020-02-22 14:57:19 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
2020-05-09 20:45:04 +02:00
|
|
|
send_email_with_rate_control(
|
|
|
|
user,
|
|
|
|
ALERT_BOUNCE_EMAIL,
|
2020-03-15 12:15:11 +01:00
|
|
|
# use user mail here as only user is authenticated to see the refused email
|
|
|
|
user.email,
|
2020-04-05 11:58:13 +02:00
|
|
|
f"Alias {address} has been disabled due to second undelivered email from {contact.website_email}",
|
2020-02-22 14:57:19 +01:00
|
|
|
render(
|
|
|
|
"transactional/automatic-disable-alias.txt",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
2020-03-17 10:56:59 +01:00
|
|
|
website_email=contact.website_email,
|
2020-03-15 10:50:46 +01:00
|
|
|
refused_email_url=refused_email_url,
|
2020-05-10 17:53:37 +02:00
|
|
|
mailbox_email=mailbox.email,
|
2020-02-22 14:57:19 +01:00
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/automatic-disable-alias.html",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
2020-03-17 10:56:59 +01:00
|
|
|
website_email=contact.website_email,
|
2020-03-15 10:50:46 +01:00
|
|
|
refused_email_url=refused_email_url,
|
2020-05-10 17:53:37 +02:00
|
|
|
mailbox_email=mailbox.email,
|
2020-02-22 14:57:19 +01:00
|
|
|
),
|
2020-03-15 10:50:46 +01:00
|
|
|
# cannot include bounce email as it can contain spammy text
|
|
|
|
# bounced_email=msg,
|
2020-02-22 14:57:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-30 22:05:31 +02:00
|
|
|
def handle_spam(
|
|
|
|
contact: Contact,
|
|
|
|
alias: Alias,
|
|
|
|
msg: Message,
|
|
|
|
user: User,
|
|
|
|
mailbox_email: str,
|
2020-04-27 18:18:40 +02:00
|
|
|
email_log: EmailLog,
|
2020-03-30 22:05:31 +02:00
|
|
|
):
|
|
|
|
# Store the report & original email
|
|
|
|
orig_msg = get_orig_message_from_spamassassin_report(msg)
|
|
|
|
# generate a name for the email
|
|
|
|
random_name = str(uuid.uuid4())
|
|
|
|
|
2020-04-02 18:09:05 +02:00
|
|
|
full_report_path = f"spams/full-{random_name}.eml"
|
2020-03-30 22:05:31 +02:00
|
|
|
s3.upload_email_from_bytesio(full_report_path, BytesIO(msg.as_bytes()), random_name)
|
|
|
|
|
|
|
|
file_path = None
|
|
|
|
if orig_msg:
|
2020-04-02 18:09:05 +02:00
|
|
|
file_path = f"spams/{random_name}.eml"
|
2020-03-30 22:05:31 +02:00
|
|
|
s3.upload_email_from_bytesio(
|
|
|
|
file_path, BytesIO(orig_msg.as_bytes()), random_name
|
|
|
|
)
|
|
|
|
|
|
|
|
refused_email = RefusedEmail.create(
|
|
|
|
path=file_path, full_report_path=full_report_path, user_id=user.id
|
|
|
|
)
|
|
|
|
db.session.flush()
|
|
|
|
|
|
|
|
email_log.refused_email_id = refused_email.id
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
LOG.d("Create spam email %s", refused_email)
|
|
|
|
|
2020-03-30 22:12:35 +02:00
|
|
|
refused_email_url = (
|
|
|
|
URL + f"/dashboard/refused_email?highlight_id=" + str(email_log.id)
|
|
|
|
)
|
2020-03-30 22:05:31 +02:00
|
|
|
disable_alias_link = f"{URL}/dashboard/unsubscribe/{alias.id}"
|
|
|
|
|
|
|
|
# inform user
|
|
|
|
LOG.d(
|
|
|
|
"Inform user %s about spam email sent by %s to alias %s",
|
|
|
|
user,
|
2020-04-05 12:00:01 +02:00
|
|
|
contact.website_email,
|
2020-03-30 22:05:31 +02:00
|
|
|
alias.email,
|
|
|
|
)
|
2020-05-09 20:45:04 +02:00
|
|
|
send_email_with_rate_control(
|
|
|
|
user,
|
|
|
|
ALERT_SPAM_EMAIL,
|
2020-03-30 22:05:31 +02:00
|
|
|
mailbox_email,
|
2020-04-05 12:00:01 +02:00
|
|
|
f"Email from {contact.website_email} to {alias.email} is detected as spam",
|
2020-03-30 22:05:31 +02:00
|
|
|
render(
|
|
|
|
"transactional/spam-email.txt",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
|
|
|
website_email=contact.website_email,
|
|
|
|
disable_alias_link=disable_alias_link,
|
|
|
|
refused_email_url=refused_email_url,
|
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/spam-email.html",
|
|
|
|
name=user.name,
|
|
|
|
alias=alias,
|
|
|
|
website_email=contact.website_email,
|
|
|
|
disable_alias_link=disable_alias_link,
|
|
|
|
refused_email_url=refused_email_url,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-02 18:10:08 +02:00
|
|
|
def handle_unsubscribe(envelope: Envelope):
|
|
|
|
msg = email.message_from_bytes(envelope.original_content)
|
2020-03-28 23:19:25 +01:00
|
|
|
|
|
|
|
# format: alias_id:
|
|
|
|
subject = msg["Subject"]
|
|
|
|
try:
|
2020-04-24 09:09:11 +02:00
|
|
|
# subject has the format {alias.id}=
|
|
|
|
if subject.endswith("="):
|
|
|
|
alias_id = int(subject[:-1])
|
|
|
|
# some email providers might strip off the = suffix
|
|
|
|
else:
|
|
|
|
alias_id = int(subject)
|
|
|
|
|
2020-03-28 23:19:25 +01:00
|
|
|
alias = Alias.get(alias_id)
|
|
|
|
except Exception:
|
|
|
|
LOG.warning("Cannot parse alias from subject %s", msg["Subject"])
|
2020-04-13 19:33:45 +02:00
|
|
|
return "550 SL E8"
|
2020-03-28 23:19:25 +01:00
|
|
|
|
|
|
|
if not alias:
|
|
|
|
LOG.warning("No such alias %s", alias_id)
|
2020-04-13 19:33:45 +02:00
|
|
|
return "550 SL E9"
|
2020-03-28 23:19:25 +01:00
|
|
|
|
|
|
|
# This sender cannot unsubscribe
|
2020-05-10 18:23:43 +02:00
|
|
|
mail_from = envelope.mail_from.lower().strip()
|
|
|
|
mailbox = Mailbox.get_by(user_id=alias.user_id, email=mail_from)
|
|
|
|
if not mailbox or mailbox not in alias.mailboxes:
|
2020-03-28 23:19:25 +01:00
|
|
|
LOG.d("%s cannot disable alias %s", envelope.mail_from, alias)
|
2020-04-13 19:33:45 +02:00
|
|
|
return "550 SL E10"
|
2020-03-28 23:19:25 +01:00
|
|
|
|
|
|
|
# Sender is owner of this alias
|
|
|
|
alias.enabled = False
|
|
|
|
db.session.commit()
|
|
|
|
user = alias.user
|
|
|
|
|
|
|
|
enable_alias_url = URL + f"/dashboard/?highlight_alias_id={alias.id}"
|
2020-05-10 18:23:43 +02:00
|
|
|
for mailbox in alias.mailboxes:
|
|
|
|
send_email(
|
|
|
|
mailbox.email,
|
|
|
|
f"Alias {alias.email} has been disabled successfully",
|
|
|
|
render(
|
|
|
|
"transactional/unsubscribe-disable-alias.txt",
|
|
|
|
user=user,
|
|
|
|
alias=alias.email,
|
|
|
|
enable_alias_url=enable_alias_url,
|
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/unsubscribe-disable-alias.html",
|
|
|
|
user=user,
|
|
|
|
alias=alias.email,
|
|
|
|
enable_alias_url=enable_alias_url,
|
|
|
|
),
|
|
|
|
)
|
2020-03-28 23:19:25 +01:00
|
|
|
|
|
|
|
return "250 Unsubscribe request accepted"
|
|
|
|
|
|
|
|
|
2020-04-04 16:09:24 +02:00
|
|
|
def handle(envelope: Envelope, smtp: SMTP) -> str:
|
|
|
|
"""Return SMTP status"""
|
|
|
|
# unsubscribe request
|
|
|
|
if UNSUBSCRIBER and envelope.rcpt_tos == [UNSUBSCRIBER]:
|
|
|
|
LOG.d("Handle unsubscribe request from %s", envelope.mail_from)
|
|
|
|
return handle_unsubscribe(envelope)
|
|
|
|
|
2020-04-04 16:27:22 +02:00
|
|
|
# Whether it's necessary to apply greylisting
|
|
|
|
if greylisting_needed(envelope.mail_from, envelope.rcpt_tos):
|
2020-04-04 21:59:42 +02:00
|
|
|
LOG.warning(
|
2020-04-04 16:27:22 +02:00
|
|
|
"Grey listing applied for %s %s", envelope.mail_from, envelope.rcpt_tos
|
|
|
|
)
|
|
|
|
return "421 SL Retry later"
|
|
|
|
|
2020-04-04 16:09:24 +02:00
|
|
|
# result of all deliveries
|
|
|
|
# each element is a couple of whether the delivery is successful and the smtp status
|
|
|
|
res: [(bool, str)] = []
|
|
|
|
|
|
|
|
for rcpt_to in envelope.rcpt_tos:
|
|
|
|
msg = email.message_from_bytes(envelope.original_content)
|
|
|
|
|
|
|
|
# Reply case
|
|
|
|
# recipient starts with "reply+" or "ra+" (ra=reverse-alias) prefix
|
|
|
|
if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
|
|
|
|
LOG.debug(">>> Reply phase %s -> %s", envelope.mail_from, rcpt_to)
|
|
|
|
is_delivered, smtp_status = handle_reply(envelope, smtp, msg, rcpt_to)
|
|
|
|
res.append((is_delivered, smtp_status))
|
|
|
|
else: # Forward case
|
|
|
|
LOG.debug(">>> Forward phase %s -> %s", envelope.mail_from, rcpt_to)
|
2020-05-10 16:57:47 +02:00
|
|
|
for is_delivered, smtp_status in handle_forward(
|
|
|
|
envelope, smtp, msg, rcpt_to
|
|
|
|
):
|
|
|
|
res.append((is_delivered, smtp_status))
|
2020-04-04 16:09:24 +02:00
|
|
|
|
|
|
|
for (is_success, smtp_status) in res:
|
|
|
|
# Consider all deliveries successful if 1 delivery is successful
|
|
|
|
if is_success:
|
|
|
|
return smtp_status
|
|
|
|
|
|
|
|
# Failed delivery for all, return the first failure
|
|
|
|
return res[0][1]
|
|
|
|
|
|
|
|
|
2020-02-19 16:17:13 +01:00
|
|
|
class MailHandler:
|
2020-04-02 18:10:08 +02:00
|
|
|
async def handle_DATA(self, server, session, envelope: Envelope):
|
2020-03-28 21:24:43 +01:00
|
|
|
LOG.debug(
|
|
|
|
"===>> New message, mail from %s, rctp tos %s ",
|
|
|
|
envelope.mail_from,
|
|
|
|
envelope.rcpt_tos,
|
|
|
|
)
|
2019-12-17 20:43:31 +01:00
|
|
|
|
2020-03-03 10:48:55 +01:00
|
|
|
if POSTFIX_SUBMISSION_TLS:
|
|
|
|
smtp = SMTP(POSTFIX_SERVER, 587)
|
|
|
|
smtp.starttls()
|
|
|
|
else:
|
2020-05-21 20:43:03 +02:00
|
|
|
smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT or 25)
|
2020-03-03 10:48:55 +01:00
|
|
|
|
2020-04-04 16:09:24 +02:00
|
|
|
app = new_app()
|
|
|
|
with app.app_context():
|
|
|
|
return handle(envelope, smtp)
|
2019-11-07 17:49:26 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-14 12:45:47 +02:00
|
|
|
if LOAD_PGP_EMAIL_HANDLER:
|
|
|
|
LOG.warning("LOAD PGP keys")
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
|
|
load_pgp_public_keys(app)
|
|
|
|
|
2019-11-08 07:55:29 +01:00
|
|
|
while True:
|
2019-12-09 22:09:28 +01:00
|
|
|
time.sleep(2)
|