email_handler.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. """
  2. Handle the email *forward* and *reply*. phase. There are 3 actors:
  3. - website: 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 website.
  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: @website
  10. rcpt to: @personal_email
  11. Header:
  12. From: @website
  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: @website
  18. rcpt to: @website
  19. Header:
  20. From: alias@sl.co # so for website the email comes from alias. magic HERE
  21. To: @website
  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. - @website
  26. """
  27. import time
  28. from email.message import EmailMessage
  29. from email.parser import Parser
  30. from email.policy import SMTPUTF8
  31. from smtplib import SMTP
  32. from aiosmtpd.controller import Controller
  33. from app.config import EMAIL_DOMAIN, POSTFIX_SERVER, URL
  34. from app.email_utils import get_email_name, get_email_part
  35. from app.extensions import db
  36. from app.log import LOG
  37. from app.models import GenEmail, ForwardEmail, ForwardEmailLog
  38. from app.utils import random_words
  39. from server import create_app
  40. # fix the database connection leak issue
  41. # use this method instead of create_app
  42. def new_app():
  43. app = create_app()
  44. @app.teardown_appcontext
  45. def shutdown_session(response_or_exc):
  46. # same as shutdown_session() in flask-sqlalchemy but this is not enough
  47. db.session.remove()
  48. # dispose the engine too
  49. db.engine.dispose()
  50. return app
  51. class MailHandler:
  52. async def handle_DATA(self, server, session, envelope):
  53. LOG.debug(">>> New message <<<")
  54. LOG.debug("Mail from %s", envelope.mail_from)
  55. LOG.debug("Rcpt to %s", envelope.rcpt_tos)
  56. message_data = envelope.content.decode("utf8", errors="replace")
  57. # Only when debug
  58. # LOG.debug("Message data:\n")
  59. # LOG.debug(message_data)
  60. # host IP, setup via Docker network
  61. smtp = SMTP(POSTFIX_SERVER, 25)
  62. msg = Parser(policy=SMTPUTF8).parsestr(message_data)
  63. # Reply case
  64. # reply+ or ra+ (reverse-alias) prefix
  65. if envelope.rcpt_tos[0].startswith("reply+") or envelope.rcpt_tos[0].startswith(
  66. "ra+"
  67. ):
  68. LOG.debug("Reply phase")
  69. app = new_app()
  70. with app.app_context():
  71. return self.handle_reply(envelope, smtp, msg)
  72. else: # Forward case
  73. LOG.debug("Forward phase")
  74. app = new_app()
  75. with app.app_context():
  76. return self.handle_forward(envelope, smtp, msg)
  77. def handle_forward(self, envelope, smtp, msg: EmailMessage) -> str:
  78. """return *status_code message*"""
  79. alias = envelope.rcpt_tos[0] # alias@SL
  80. gen_email = GenEmail.get_by(email=alias)
  81. if not gen_email:
  82. LOG.d("alias %s not exist")
  83. return "510 Email not exist"
  84. user_email = gen_email.user.email
  85. website_email = get_email_part(msg["From"])
  86. forward_email = ForwardEmail.get_by(
  87. gen_email_id=gen_email.id, website_email=website_email
  88. )
  89. if not forward_email:
  90. LOG.debug(
  91. "create forward email for alias %s and website email %s",
  92. alias,
  93. website_email,
  94. )
  95. # generate a reply_email, make sure it is unique
  96. # not use while to avoid infinite loop
  97. for _ in range(1000):
  98. reply_email = f"reply+{random_words()}@{EMAIL_DOMAIN}"
  99. if not ForwardEmail.get_by(reply_email=reply_email):
  100. break
  101. forward_email = ForwardEmail.create(
  102. gen_email_id=gen_email.id,
  103. website_email=website_email,
  104. website_from=msg["From"],
  105. reply_email=reply_email,
  106. )
  107. db.session.commit()
  108. forward_log = ForwardEmailLog.create(forward_id=forward_email.id)
  109. if gen_email.enabled:
  110. # add custom header
  111. add_or_replace_header(msg, "X-SimpleLogin-Type", "Forward")
  112. # remove reply-to header if present
  113. if msg["Reply-To"]:
  114. LOG.d("Delete reply-to header %s", msg["Reply-To"])
  115. del msg["Reply-To"]
  116. # change the from header so the sender comes from @SL
  117. # so it can pass DMARC check
  118. # replace the email part in from: header
  119. from_header = (
  120. get_email_name(msg["From"])
  121. + " - "
  122. + website_email.replace("@", " at ")
  123. + f" <{forward_email.reply_email}>"
  124. )
  125. msg.replace_header("From", from_header)
  126. LOG.d("new from header:%s", from_header)
  127. # add List-Unsubscribe header
  128. unsubscribe_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}"
  129. add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
  130. add_or_replace_header(
  131. msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
  132. )
  133. # remove DKIM-Signature as Postfix will add this header
  134. if msg["DKIM-Signature"]:
  135. LOG.d("Remove DKIM-Signature %s", msg["DKIM-Signature"])
  136. del msg["DKIM-Signature"]
  137. original_subject = msg["Subject"]
  138. LOG.d(
  139. "Forward mail from %s to %s, subject %s, mail_options %s, rcpt_options %s ",
  140. website_email,
  141. user_email,
  142. original_subject,
  143. envelope.mail_options,
  144. envelope.rcpt_options,
  145. )
  146. # smtp.send_message has UnicodeEncodeErroremail issue
  147. # encode message raw directly instead
  148. msg_raw = msg.as_string().encode()
  149. smtp.sendmail(
  150. forward_email.reply_email,
  151. user_email,
  152. msg_raw,
  153. envelope.mail_options,
  154. envelope.rcpt_options,
  155. )
  156. # smtp.send_message(
  157. # msg,
  158. # from_addr=forward_email.reply_email,
  159. # to_addrs=[user_email], # user personal email
  160. # mail_options=envelope.mail_options,
  161. # rcpt_options=envelope.rcpt_options,
  162. # )
  163. else:
  164. LOG.d("%s is disabled, do not forward", gen_email)
  165. forward_log.blocked = True
  166. db.session.commit()
  167. return "250 Message accepted for delivery"
  168. def handle_reply(self, envelope, smtp, msg: EmailMessage) -> str:
  169. reply_email = envelope.rcpt_tos[0]
  170. # reply_email must end with EMAIL_DOMAIN
  171. if not reply_email.endswith(EMAIL_DOMAIN):
  172. LOG.error(f"Reply email {reply_email} has wrong domain")
  173. return "550 wrong reply email"
  174. forward_email = ForwardEmail.get_by(reply_email=reply_email)
  175. alias: str = forward_email.gen_email.email
  176. # todo: add DKIM-Signature for custom domain
  177. # remove DKIM-Signature for custom domain
  178. if not alias.endswith(EMAIL_DOMAIN) and msg["DKIM-Signature"]:
  179. LOG.d(
  180. "Remove DKIM-Signature %s for custom-domain alias %s",
  181. msg["DKIM-Signature"],
  182. alias,
  183. )
  184. del msg["DKIM-Signature"]
  185. # email seems to come from alias
  186. msg.replace_header("From", alias)
  187. msg.replace_header("To", forward_email.website_email)
  188. # add List-Unsubscribe header
  189. unsubscribe_link = f"{URL}/dashboard/unsubscribe/{forward_email.gen_email_id}"
  190. add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
  191. add_or_replace_header(
  192. msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
  193. )
  194. LOG.d(
  195. "send email from %s to %s, mail_options:%s,rcpt_options:%s",
  196. alias,
  197. forward_email.website_email,
  198. envelope.mail_options,
  199. envelope.rcpt_options,
  200. )
  201. msg_raw = msg.as_string().encode()
  202. smtp.sendmail(
  203. alias,
  204. forward_email.website_email,
  205. msg_raw,
  206. envelope.mail_options,
  207. envelope.rcpt_options,
  208. )
  209. ForwardEmailLog.create(forward_id=forward_email.id, is_reply=True)
  210. db.session.commit()
  211. return "250 Message accepted for delivery"
  212. def add_or_replace_header(msg: EmailMessage, header: str, value: str):
  213. try:
  214. msg.add_header(header, value)
  215. except ValueError:
  216. # the header exists already
  217. msg.replace_header(header, value)
  218. if __name__ == "__main__":
  219. controller = Controller(MailHandler(), hostname="0.0.0.0", port=20381)
  220. controller.start()
  221. LOG.d("Start mail controller %s %s", controller.hostname, controller.port)
  222. while True:
  223. time.sleep(2)