email_handler.py 27 KB

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