email_handler.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. """
  2. Handle the email *forward* and *reply*. phase. There are 3 actors:
  3. - contact: who sends emails to alias@sl.co address
  4. - SL email handler (this script)
  5. - user personal email: to be protected. Should never leak to contact.
  6. This script makes sure that in the forward phase, the email that is forwarded to user personal email has the following
  7. envelope and header fields:
  8. Envelope:
  9. mail from: @contact
  10. rcpt to: @personal_email
  11. Header:
  12. From: @contact
  13. To: alias@sl.co # so user knows this email is sent to alias
  14. Reply-to: special@sl.co # magic HERE
  15. And in the reply phase:
  16. Envelope:
  17. mail from: @contact
  18. rcpt to: @contact
  19. Header:
  20. From: alias@sl.co # so for contact the email comes from alias. magic HERE
  21. To: @contact
  22. The special@sl.co allows to hide user personal email when user clicks "Reply" to the forwarded email.
  23. It should contain the following info:
  24. - alias
  25. - @contact
  26. """
  27. import time
  28. import uuid
  29. from email import encoders
  30. from email.message import Message
  31. from email.mime.application import MIMEApplication
  32. from email.mime.multipart import MIMEMultipart
  33. from email.parser import Parser
  34. from email.policy import SMTPUTF8
  35. from email.utils import parseaddr
  36. from io import BytesIO
  37. from smtplib import SMTP
  38. from typing import Optional
  39. from aiosmtpd.controller import Controller
  40. from app import pgp_utils, s3
  41. from app.config import (
  42. EMAIL_DOMAIN,
  43. POSTFIX_SERVER,
  44. URL,
  45. ALIAS_DOMAINS,
  46. POSTFIX_SUBMISSION_TLS,
  47. UNSUBSCRIBER,
  48. )
  49. from app.email_utils import (
  50. send_email,
  51. add_dkim_signature,
  52. get_email_domain_part,
  53. add_or_replace_header,
  54. delete_header,
  55. send_cannot_create_directory_alias,
  56. send_cannot_create_domain_alias,
  57. email_belongs_to_alias_domains,
  58. render,
  59. get_orig_message_from_bounce,
  60. delete_all_headers_except,
  61. new_addr,
  62. get_addrs_from_header,
  63. get_spam_info,
  64. get_orig_message_from_spamassassin_report,
  65. )
  66. from app.extensions import db
  67. from app.log import LOG
  68. from app.models import (
  69. Alias,
  70. Contact,
  71. EmailLog,
  72. CustomDomain,
  73. Directory,
  74. User,
  75. DeletedAlias,
  76. RefusedEmail,
  77. )
  78. from app.utils import random_string
  79. from server import create_app
  80. # fix the database connection leak issue
  81. # use this method instead of create_app
  82. def new_app():
  83. app = create_app()
  84. @app.teardown_appcontext
  85. def shutdown_session(response_or_exc):
  86. # same as shutdown_session() in flask-sqlalchemy but this is not enough
  87. db.session.remove()
  88. # dispose the engine too
  89. db.engine.dispose()
  90. return app
  91. def try_auto_create(address: str) -> Optional[Alias]:
  92. """Try to auto-create the alias using directory or catch-all domain
  93. """
  94. alias = try_auto_create_catch_all_domain(address)
  95. if not alias:
  96. alias = try_auto_create_directory(address)
  97. return alias
  98. def try_auto_create_directory(address: str) -> Optional[Alias]:
  99. """
  100. Try to create an alias with directory
  101. """
  102. # check if alias belongs to a directory, ie having directory/anything@EMAIL_DOMAIN format
  103. if email_belongs_to_alias_domains(address):
  104. # if there's no directory separator in the alias, no way to auto-create it
  105. if "/" not in address and "+" not in address and "#" not in address:
  106. return None
  107. # alias contains one of the 3 special directory separator: "/", "+" or "#"
  108. if "/" in address:
  109. sep = "/"
  110. elif "+" in address:
  111. sep = "+"
  112. else:
  113. sep = "#"
  114. directory_name = address[: address.find(sep)]
  115. LOG.d("directory_name %s", directory_name)
  116. directory = Directory.get_by(name=directory_name)
  117. if not directory:
  118. return None
  119. dir_user: User = directory.user
  120. if not dir_user.can_create_new_alias():
  121. send_cannot_create_directory_alias(dir_user, address, directory_name)
  122. return None
  123. # if alias has been deleted before, do not auto-create it
  124. if DeletedAlias.get_by(email=address, user_id=directory.user_id):
  125. LOG.warning(
  126. "Alias %s was deleted before, cannot auto-create using directory %s, user %s",
  127. address,
  128. directory_name,
  129. dir_user,
  130. )
  131. return None
  132. LOG.d("create alias %s for directory %s", address, directory)
  133. alias = Alias.create(
  134. email=address,
  135. user_id=directory.user_id,
  136. directory_id=directory.id,
  137. mailbox_id=dir_user.default_mailbox_id,
  138. )
  139. db.session.commit()
  140. return alias
  141. def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
  142. """Try to create an alias with catch-all domain"""
  143. # try to create alias on-the-fly with custom-domain catch-all feature
  144. # check if alias is custom-domain alias and if the custom-domain has catch-all enabled
  145. alias_domain = get_email_domain_part(address)
  146. custom_domain = CustomDomain.get_by(domain=alias_domain)
  147. if not custom_domain:
  148. return None
  149. # custom_domain exists
  150. if not custom_domain.catch_all:
  151. return None
  152. # custom_domain has catch-all enabled
  153. domain_user: User = custom_domain.user
  154. if not domain_user.can_create_new_alias():
  155. send_cannot_create_domain_alias(domain_user, address, alias_domain)
  156. return None
  157. # if alias has been deleted before, do not auto-create it
  158. if DeletedAlias.get_by(email=address, user_id=custom_domain.user_id):
  159. LOG.warning(
  160. "Alias %s was deleted before, cannot auto-create using domain catch-all %s, user %s",
  161. address,
  162. custom_domain,
  163. domain_user,
  164. )
  165. return None
  166. LOG.d("create alias %s for domain %s", address, custom_domain)
  167. alias = Alias.create(
  168. email=address,
  169. user_id=custom_domain.user_id,
  170. custom_domain_id=custom_domain.id,
  171. automatic_creation=True,
  172. mailbox_id=domain_user.default_mailbox_id,
  173. )
  174. db.session.commit()
  175. return alias
  176. def get_or_create_contact(website_from_header: str, alias: Alias) -> Contact:
  177. """
  178. website_from_header can be the full-form email, i.e. "First Last <email@example.com>"
  179. """
  180. _, website_email = parseaddr(website_from_header)
  181. contact = Contact.get_by(alias_id=alias.id, website_email=website_email)
  182. if contact:
  183. # update the website_from if needed
  184. if contact.website_from != website_from_header:
  185. LOG.d("Update From header for %s", contact)
  186. contact.website_from = website_from_header
  187. db.session.commit()
  188. else:
  189. LOG.debug(
  190. "create forward email for alias %s and website email %s",
  191. alias,
  192. website_from_header,
  193. )
  194. reply_email = generate_reply_email()
  195. contact = Contact.create(
  196. user_id=alias.user_id,
  197. alias_id=alias.id,
  198. website_email=website_email,
  199. website_from=website_from_header,
  200. reply_email=reply_email,
  201. )
  202. db.session.commit()
  203. return contact
  204. def replace_header_when_forward(msg: Message, alias: Alias, header: str):
  205. """
  206. Replace CC or To header by Reply emails in forward phase
  207. """
  208. addrs = get_addrs_from_header(msg, header)
  209. # Nothing to do
  210. if not addrs:
  211. return
  212. new_addrs: [str] = []
  213. need_replace = False
  214. for addr in addrs:
  215. name, email = parseaddr(addr)
  216. # no transformation when alias is already in the header
  217. if email == alias.email:
  218. new_addrs.append(addr)
  219. continue
  220. contact = Contact.get_by(alias_id=alias.id, website_email=email)
  221. if contact:
  222. # update the website_from if needed
  223. if contact.website_from != addr:
  224. LOG.d("Update website_from for %s to %s", contact, addr)
  225. contact.website_from = addr
  226. db.session.commit()
  227. else:
  228. LOG.debug(
  229. "create contact for alias %s and email %s, header %s",
  230. alias,
  231. email,
  232. header,
  233. )
  234. reply_email = generate_reply_email()
  235. contact = Contact.create(
  236. user_id=alias.user_id,
  237. alias_id=alias.id,
  238. website_email=email,
  239. website_from=addr,
  240. reply_email=reply_email,
  241. is_cc=header.lower() == "cc",
  242. )
  243. db.session.commit()
  244. new_addrs.append(
  245. new_addr(contact.website_from, contact.reply_email, alias.user)
  246. )
  247. need_replace = True
  248. if need_replace:
  249. new_header = ",".join(new_addrs)
  250. LOG.d("Replace %s header, old: %s, new: %s", header, msg[header], new_header)
  251. add_or_replace_header(msg, header, new_header)
  252. else:
  253. LOG.d("No need to replace %s header", header)
  254. def replace_header_when_reply(msg: Message, alias: Alias, header: str):
  255. """
  256. Replace CC or To Reply emails by original emails
  257. """
  258. addrs = get_addrs_from_header(msg, header)
  259. # Nothing to do
  260. if not addrs:
  261. return
  262. new_addrs: [str] = []
  263. for addr in addrs:
  264. name, email = parseaddr(addr)
  265. # no transformation when alias is already in the header
  266. if email == alias.email:
  267. continue
  268. contact = Contact.get_by(reply_email=email)
  269. if not contact:
  270. LOG.warning(
  271. "%s email in reply phase %s must be reply emails", header, email
  272. )
  273. # still keep this email in header
  274. new_addrs.append(addr)
  275. continue
  276. new_addrs.append(contact.website_from or contact.website_email)
  277. new_header = ",".join(new_addrs)
  278. LOG.d("Replace %s header, old: %s, new: %s", header, msg[header], new_header)
  279. add_or_replace_header(msg, header, new_header)
  280. def generate_reply_email():
  281. # generate a reply_email, make sure it is unique
  282. # not use while loop to avoid infinite loop
  283. reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}"
  284. for _ in range(1000):
  285. if not Contact.get_by(reply_email=reply_email):
  286. # found!
  287. break
  288. reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}"
  289. return reply_email
  290. def should_append_alias(msg: Message, address: str):
  291. """whether an alias should be appended to TO header in message"""
  292. if msg["To"] and address in msg["To"]:
  293. return False
  294. if msg["Cc"] and address in msg["Cc"]:
  295. return False
  296. return True
  297. def prepare_pgp_message(orig_msg: Message, pgp_fingerprint: str):
  298. msg = MIMEMultipart("encrypted", protocol="application/pgp-encrypted")
  299. # copy all headers from original message except the "Content-Type"
  300. for i in reversed(range(len(orig_msg._headers))):
  301. header_name = orig_msg._headers[i][0].lower()
  302. if header_name != "Content-Type".lower():
  303. msg[header_name] = orig_msg._headers[i][1]
  304. # Delete unnecessary headers in orig_msg except to save space
  305. delete_all_headers_except(
  306. orig_msg,
  307. [
  308. "MIME-Version",
  309. "Content-Type",
  310. "Content-Disposition",
  311. "Content-Transfer-Encoding",
  312. ],
  313. )
  314. first = MIMEApplication(
  315. _subtype="pgp-encrypted", _encoder=encoders.encode_7or8bit, _data=""
  316. )
  317. first.set_payload("Version: 1")
  318. msg.attach(first)
  319. second = MIMEApplication("octet-stream", _encoder=encoders.encode_7or8bit)
  320. second.add_header("Content-Disposition", "inline")
  321. # encrypt original message
  322. encrypted_data = pgp_utils.encrypt(orig_msg.as_string(), pgp_fingerprint)
  323. second.set_payload(encrypted_data)
  324. msg.attach(second)
  325. return msg
  326. def handle_forward(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> (bool, str):
  327. """return whether an email has been delivered and
  328. the smtp status ("250 Message accepted", "550 Non-existent email address", etc)
  329. """
  330. address = rcpt_to.lower() # alias@SL
  331. alias = Alias.get_by(email=address)
  332. if not alias:
  333. LOG.d("alias %s not exist. Try to see if it can be created on the fly", address)
  334. alias = try_auto_create(address)
  335. if not alias:
  336. LOG.d("alias %s cannot be created on-the-fly, return 550", address)
  337. return False, "550 SL Email not exist"
  338. mailbox = alias.mailbox
  339. mailbox_email = mailbox.email
  340. user = alias.user
  341. # Sometimes when user clicks on "reply all"
  342. # an email is sent to the same alias that the previous message is destined to
  343. if envelope.mail_from == mailbox_email:
  344. # nothing to do
  345. LOG.d("Forward from %s to %s, nothing to do", envelope.mail_from, mailbox_email)
  346. return False, "550 SL ignored"
  347. contact = get_or_create_contact(msg["From"], alias)
  348. spam_check = True
  349. # create PGP email if needed
  350. if mailbox.pgp_finger_print and user.is_premium():
  351. LOG.d("Encrypt message using mailbox %s", mailbox)
  352. msg = prepare_pgp_message(msg, mailbox.pgp_finger_print)
  353. # no need to spam check for encrypted message
  354. spam_check = False
  355. if spam_check:
  356. is_spam, spam_status = get_spam_info(msg)
  357. if is_spam:
  358. LOG.warning("Email detected as spam. Alias: %s, from: %s", alias, contact)
  359. handle_spam(contact, alias, msg, user, mailbox_email, spam_status)
  360. return False, "550 SL ignored"
  361. forward_log = EmailLog.create(contact_id=contact.id, user_id=contact.user_id)
  362. if alias.enabled:
  363. # add custom header
  364. add_or_replace_header(msg, "X-SimpleLogin-Type", "Forward")
  365. # remove reply-to & sender header if present
  366. delete_header(msg, "Reply-To")
  367. delete_header(msg, "Sender")
  368. # change the from header so the sender comes from @SL
  369. # so it can pass DMARC check
  370. # replace the email part in from: header
  371. contact_from_header = msg["From"]
  372. contact_name, contact_email = parseaddr(contact_from_header)
  373. new_from_header = new_addr(contact_from_header, contact.reply_email, user)
  374. add_or_replace_header(msg, "From", new_from_header)
  375. LOG.d("new_from_header:%s, old header %s", new_from_header, contact_from_header)
  376. # replace CC & To emails by reply-email for all emails that are not alias
  377. replace_header_when_forward(msg, alias, "Cc")
  378. replace_header_when_forward(msg, alias, "To")
  379. # append alias into the TO header if it's not present in To or CC
  380. if should_append_alias(msg, alias.email):
  381. LOG.d("append alias %s to TO header %s", alias, msg["To"])
  382. if msg["To"]:
  383. to_header = msg["To"] + "," + alias.email
  384. else:
  385. to_header = alias.email
  386. add_or_replace_header(msg, "To", to_header.strip())
  387. # add List-Unsubscribe header
  388. if UNSUBSCRIBER:
  389. unsubscribe_link = f"mailto:{UNSUBSCRIBER}?subject={alias.id}="
  390. add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
  391. else:
  392. unsubscribe_link = f"{URL}/dashboard/unsubscribe/{alias.id}"
  393. add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
  394. add_or_replace_header(
  395. msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
  396. )
  397. add_dkim_signature(msg, EMAIL_DOMAIN)
  398. LOG.d(
  399. "Forward mail from %s to %s, mail_options %s, rcpt_options %s ",
  400. contact_email,
  401. mailbox_email,
  402. envelope.mail_options,
  403. envelope.rcpt_options,
  404. )
  405. # smtp.send_message has UnicodeEncodeErroremail issue
  406. # encode message raw directly instead
  407. msg_raw = msg.as_string().encode()
  408. smtp.sendmail(
  409. contact.reply_email,
  410. mailbox_email,
  411. msg_raw,
  412. envelope.mail_options,
  413. envelope.rcpt_options,
  414. )
  415. else:
  416. LOG.d("%s is disabled, do not forward", alias)
  417. forward_log.blocked = True
  418. db.session.commit()
  419. return True, "250 Message accepted for delivery"
  420. def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> (bool, str):
  421. """
  422. return whether an email has been delivered and
  423. the smtp status ("250 Message accepted", "550 Non-existent email address", etc)
  424. """
  425. reply_email = rcpt_to.lower()
  426. # reply_email must end with EMAIL_DOMAIN
  427. if not reply_email.endswith(EMAIL_DOMAIN):
  428. LOG.warning(f"Reply email {reply_email} has wrong domain")
  429. return False, "550 SL wrong reply email"
  430. contact = Contact.get_by(reply_email=reply_email)
  431. if not contact:
  432. LOG.warning(f"No such forward-email with {reply_email} as reply-email")
  433. return False, "550 SL wrong reply email"
  434. alias = contact.alias
  435. address: str = contact.alias.email
  436. alias_domain = address[address.find("@") + 1 :]
  437. # alias must end with one of the ALIAS_DOMAINS or custom-domain
  438. if not email_belongs_to_alias_domains(alias.email):
  439. if not CustomDomain.get_by(domain=alias_domain):
  440. return False, "550 SL alias unknown by SimpleLogin"
  441. user = alias.user
  442. mailbox_email = alias.mailbox_email()
  443. # bounce email initiated by Postfix
  444. # can happen in case emails cannot be delivered to user-email
  445. # in this case Postfix will try to send a bounce report to original sender, which is
  446. # the "reply email"
  447. if envelope.mail_from == "<>":
  448. LOG.warning(
  449. "Bounce when sending to alias %s from %s, user %s",
  450. alias,
  451. contact.website_from,
  452. alias.user,
  453. )
  454. handle_bounce(contact, alias, msg, user, mailbox_email)
  455. return False, "550 SL ignored"
  456. # only mailbox can send email to the reply-email
  457. if envelope.mail_from.lower() != mailbox_email.lower():
  458. LOG.warning(
  459. f"Reply email can only be used by mailbox. "
  460. f"Actual mail_from: %s. msg from header: %s, Mailbox %s. reply_email %s",
  461. envelope.mail_from,
  462. msg["From"],
  463. mailbox_email,
  464. reply_email,
  465. )
  466. send_email(
  467. mailbox_email,
  468. f"Reply from your alias {alias.email} only works from your mailbox",
  469. render(
  470. "transactional/reply-must-use-personal-email.txt",
  471. name=user.name,
  472. alias=alias.email,
  473. sender=envelope.mail_from,
  474. mailbox_email=mailbox_email,
  475. ),
  476. render(
  477. "transactional/reply-must-use-personal-email.html",
  478. name=user.name,
  479. alias=alias.email,
  480. sender=envelope.mail_from,
  481. mailbox_email=mailbox_email,
  482. ),
  483. )
  484. # Notify sender that they cannot send emails to this address
  485. send_email(
  486. envelope.mail_from,
  487. f"Your email ({envelope.mail_from}) is not allowed to send emails to {reply_email}",
  488. render(
  489. "transactional/send-from-alias-from-unknown-sender.txt",
  490. sender=envelope.mail_from,
  491. reply_email=reply_email,
  492. ),
  493. "",
  494. )
  495. return False, "550 SL ignored"
  496. delete_header(msg, "DKIM-Signature")
  497. delete_header(msg, "Received")
  498. # make the email comes from alias
  499. add_or_replace_header(msg, "From", alias.email)
  500. # some email providers like ProtonMail adds automatically the Reply-To field
  501. # make sure to delete it
  502. delete_header(msg, "Reply-To")
  503. # remove sender header if present as this could reveal user real email
  504. delete_header(msg, "Sender")
  505. replace_header_when_reply(msg, alias, "To")
  506. replace_header_when_reply(msg, alias, "Cc")
  507. # Received-SPF is injected by postfix-policyd-spf-python can reveal user original email
  508. delete_header(msg, "Received-SPF")
  509. LOG.d(
  510. "send email from %s to %s, mail_options:%s,rcpt_options:%s",
  511. alias.email,
  512. contact.website_email,
  513. envelope.mail_options,
  514. envelope.rcpt_options,
  515. )
  516. if alias_domain in ALIAS_DOMAINS:
  517. add_dkim_signature(msg, alias_domain)
  518. # add DKIM-Signature for custom-domain alias
  519. else:
  520. custom_domain: CustomDomain = CustomDomain.get_by(domain=alias_domain)
  521. if custom_domain.dkim_verified:
  522. add_dkim_signature(msg, alias_domain)
  523. msg_raw = msg.as_string().encode()
  524. smtp.sendmail(
  525. alias.email,
  526. contact.website_email,
  527. msg_raw,
  528. envelope.mail_options,
  529. envelope.rcpt_options,
  530. )
  531. EmailLog.create(contact_id=contact.id, is_reply=True, user_id=contact.user_id)
  532. db.session.commit()
  533. return True, "250 Message accepted for delivery"
  534. def handle_bounce(
  535. contact: Contact, alias: Alias, msg: Message, user: User, mailbox_email: str
  536. ):
  537. address = alias.email
  538. fel: EmailLog = EmailLog.create(
  539. contact_id=contact.id, bounced=True, user_id=contact.user_id
  540. )
  541. db.session.commit()
  542. nb_bounced = EmailLog.filter_by(contact_id=contact.id, bounced=True).count()
  543. disable_alias_link = f"{URL}/dashboard/unsubscribe/{alias.id}"
  544. # Store the bounced email
  545. orig_msg = get_orig_message_from_bounce(msg)
  546. # generate a name for the email
  547. random_name = str(uuid.uuid4())
  548. full_report_path = f"refused-emails/full-{random_name}.eml"
  549. s3.upload_email_from_bytesio(full_report_path, BytesIO(msg.as_bytes()), random_name)
  550. file_path = None
  551. if orig_msg:
  552. file_path = f"refused-emails/{random_name}.eml"
  553. s3.upload_email_from_bytesio(
  554. file_path, BytesIO(orig_msg.as_bytes()), random_name
  555. )
  556. refused_email = RefusedEmail.create(
  557. path=file_path, full_report_path=full_report_path, user_id=user.id
  558. )
  559. db.session.flush()
  560. fel.refused_email_id = refused_email.id
  561. db.session.commit()
  562. LOG.d("Create refused email %s", refused_email)
  563. refused_email_url = URL + f"/dashboard/refused_email?highlight_id=" + str(fel.id)
  564. # inform user if this is the first bounced email
  565. if nb_bounced == 1:
  566. LOG.d(
  567. "Inform user %s about bounced email sent by %s to alias %s",
  568. user,
  569. contact.website_from,
  570. address,
  571. )
  572. send_email(
  573. # use user mail here as only user is authenticated to see the refused email
  574. user.email,
  575. f"Email from {contact.website_from} to {address} cannot be delivered to your inbox",
  576. render(
  577. "transactional/bounced-email.txt",
  578. name=user.name,
  579. alias=alias,
  580. website_from=contact.website_from,
  581. website_email=contact.website_email,
  582. disable_alias_link=disable_alias_link,
  583. refused_email_url=refused_email_url,
  584. mailbox_email=mailbox_email,
  585. ),
  586. render(
  587. "transactional/bounced-email.html",
  588. name=user.name,
  589. alias=alias,
  590. website_from=contact.website_from,
  591. website_email=contact.website_email,
  592. disable_alias_link=disable_alias_link,
  593. refused_email_url=refused_email_url,
  594. mailbox_email=mailbox_email,
  595. ),
  596. # cannot include bounce email as it can contain spammy text
  597. # bounced_email=msg,
  598. )
  599. # disable the alias the second time email is bounced
  600. elif nb_bounced >= 2:
  601. LOG.d(
  602. "Bounce happens again with alias %s from %s. Disable alias now ",
  603. address,
  604. contact.website_from,
  605. )
  606. alias.enabled = False
  607. db.session.commit()
  608. send_email(
  609. # use user mail here as only user is authenticated to see the refused email
  610. user.email,
  611. f"Alias {address} has been disabled due to second undelivered email from {contact.website_from}",
  612. render(
  613. "transactional/automatic-disable-alias.txt",
  614. name=user.name,
  615. alias=alias,
  616. website_from=contact.website_from,
  617. website_email=contact.website_email,
  618. refused_email_url=refused_email_url,
  619. mailbox_email=mailbox_email,
  620. ),
  621. render(
  622. "transactional/automatic-disable-alias.html",
  623. name=user.name,
  624. alias=alias,
  625. website_from=contact.website_from,
  626. website_email=contact.website_email,
  627. refused_email_url=refused_email_url,
  628. mailbox_email=mailbox_email,
  629. ),
  630. # cannot include bounce email as it can contain spammy text
  631. # bounced_email=msg,
  632. )
  633. def handle_spam(
  634. contact: Contact,
  635. alias: Alias,
  636. msg: Message,
  637. user: User,
  638. mailbox_email: str,
  639. spam_status: str,
  640. ):
  641. email_log: EmailLog = EmailLog.create(
  642. contact_id=contact.id,
  643. user_id=contact.user_id,
  644. is_spam=True,
  645. spam_status=spam_status,
  646. )
  647. db.session.commit()
  648. # Store the report & original email
  649. orig_msg = get_orig_message_from_spamassassin_report(msg)
  650. # generate a name for the email
  651. random_name = str(uuid.uuid4())
  652. full_report_path = f"refused-emails/full-{random_name}.eml"
  653. s3.upload_email_from_bytesio(full_report_path, BytesIO(msg.as_bytes()), random_name)
  654. file_path = None
  655. if orig_msg:
  656. file_path = f"refused-emails/{random_name}.eml"
  657. s3.upload_email_from_bytesio(
  658. file_path, BytesIO(orig_msg.as_bytes()), random_name
  659. )
  660. refused_email = RefusedEmail.create(
  661. path=file_path, full_report_path=full_report_path, user_id=user.id
  662. )
  663. db.session.flush()
  664. email_log.refused_email_id = refused_email.id
  665. db.session.commit()
  666. LOG.d("Create spam email %s", refused_email)
  667. refused_email_url = (
  668. URL + f"/dashboard/refused_email?highlight_id=" + str(email_log.id)
  669. )
  670. disable_alias_link = f"{URL}/dashboard/unsubscribe/{alias.id}"
  671. # inform user
  672. LOG.d(
  673. "Inform user %s about spam email sent by %s to alias %s",
  674. user,
  675. contact.website_from,
  676. alias.email,
  677. )
  678. send_email(
  679. mailbox_email,
  680. f"Email from {contact.website_from} to {alias.email} is detected as spam",
  681. render(
  682. "transactional/spam-email.txt",
  683. name=user.name,
  684. alias=alias,
  685. website_from=contact.website_from,
  686. website_email=contact.website_email,
  687. disable_alias_link=disable_alias_link,
  688. refused_email_url=refused_email_url,
  689. ),
  690. render(
  691. "transactional/spam-email.html",
  692. name=user.name,
  693. alias=alias,
  694. website_from=contact.website_from,
  695. website_email=contact.website_email,
  696. disable_alias_link=disable_alias_link,
  697. refused_email_url=refused_email_url,
  698. ),
  699. )
  700. def handle_unsubscribe(envelope):
  701. message_data = envelope.content.decode("utf8", errors="replace")
  702. msg = Parser(policy=SMTPUTF8).parsestr(message_data)
  703. # format: alias_id:
  704. subject = msg["Subject"]
  705. try:
  706. alias_id = int(subject[:-1])
  707. alias = Alias.get(alias_id)
  708. except Exception:
  709. LOG.warning("Cannot parse alias from subject %s", msg["Subject"])
  710. return "550 SL ignored"
  711. if not alias:
  712. LOG.warning("No such alias %s", alias_id)
  713. return "550 SL ignored"
  714. # This sender cannot unsubscribe
  715. if alias.mailbox_email() != envelope.mail_from:
  716. LOG.d("%s cannot disable alias %s", envelope.mail_from, alias)
  717. return "550 SL ignored"
  718. # Sender is owner of this alias
  719. alias.enabled = False
  720. db.session.commit()
  721. user = alias.user
  722. enable_alias_url = URL + f"/dashboard/?highlight_alias_id={alias.id}"
  723. send_email(
  724. envelope.mail_from,
  725. f"Alias {alias.email} has been disabled successfully",
  726. render(
  727. "transactional/unsubscribe-disable-alias.txt",
  728. user=user,
  729. alias=alias.email,
  730. enable_alias_url=enable_alias_url,
  731. ),
  732. render(
  733. "transactional/unsubscribe-disable-alias.html",
  734. user=user,
  735. alias=alias.email,
  736. enable_alias_url=enable_alias_url,
  737. ),
  738. )
  739. return "250 Unsubscribe request accepted"
  740. class MailHandler:
  741. async def handle_DATA(self, server, session, envelope):
  742. LOG.debug(
  743. "===>> New message, mail from %s, rctp tos %s ",
  744. envelope.mail_from,
  745. envelope.rcpt_tos,
  746. )
  747. if POSTFIX_SUBMISSION_TLS:
  748. smtp = SMTP(POSTFIX_SERVER, 587)
  749. smtp.starttls()
  750. else:
  751. smtp = SMTP(POSTFIX_SERVER, 25)
  752. # unsubscribe request
  753. if UNSUBSCRIBER and envelope.rcpt_tos == [UNSUBSCRIBER]:
  754. LOG.d("Handle unsubscribe request from %s", envelope.mail_from)
  755. app = new_app()
  756. with app.app_context():
  757. return handle_unsubscribe(envelope)
  758. # result of all deliveries
  759. # each element is a couple of whether the delivery is successful and the smtp status
  760. res: [(bool, str)] = []
  761. for rcpt_to in envelope.rcpt_tos:
  762. message_data = envelope.content.decode("utf8", errors="replace")
  763. msg = Parser(policy=SMTPUTF8).parsestr(message_data)
  764. # Reply case
  765. # recipient starts with "reply+" or "ra+" (ra=reverse-alias) prefix
  766. if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
  767. LOG.debug(">>> Reply phase %s -> %s", envelope.mail_from, rcpt_to)
  768. app = new_app()
  769. with app.app_context():
  770. is_delivered, smtp_status = handle_reply(
  771. envelope, smtp, msg, rcpt_to
  772. )
  773. res.append((is_delivered, smtp_status))
  774. else: # Forward case
  775. LOG.debug(">>> Forward phase %s -> %s", envelope.mail_from, rcpt_to)
  776. app = new_app()
  777. with app.app_context():
  778. is_delivered, smtp_status = handle_forward(
  779. envelope, smtp, msg, rcpt_to
  780. )
  781. res.append((is_delivered, smtp_status))
  782. for (is_success, smtp_status) in res:
  783. # Consider all deliveries successful if 1 delivery is successful
  784. if is_success:
  785. return smtp_status
  786. # Failed delivery for all, return the first failure
  787. return res[0][1]
  788. if __name__ == "__main__":
  789. controller = Controller(MailHandler(), hostname="0.0.0.0", port=20381)
  790. controller.start()
  791. LOG.d("Start mail controller %s %s", controller.hostname, controller.port)
  792. while True:
  793. time.sleep(2)