email_handler.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 notify_admin
  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. class MailHandler:
  41. async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
  42. if not address.endswith(EMAIL_DOMAIN):
  43. LOG.error(f"Not handle email {address}")
  44. return "550 not relaying to that domain"
  45. envelope.rcpt_tos.append(address)
  46. return "250 OK"
  47. async def handle_DATA(self, server, session, envelope):
  48. LOG.debug(">>> New message <<<")
  49. LOG.debug("Mail from %s", envelope.mail_from)
  50. LOG.debug("Rcpt to %s", envelope.rcpt_tos)
  51. message_data = envelope.content.decode("utf8", errors="replace")
  52. # Only when debug
  53. # LOG.debug("Message data:\n")
  54. # LOG.debug(message_data)
  55. # host IP, setup via Docker network
  56. smtp = SMTP(POSTFIX_SERVER, 25)
  57. msg = Parser(policy=SMTPUTF8).parsestr(message_data)
  58. if not envelope.rcpt_tos[0].startswith("reply+"): # Forward case
  59. LOG.debug("Forward phase")
  60. app = create_app()
  61. with app.app_context():
  62. return self.handle_forward(envelope, smtp, msg)
  63. else:
  64. LOG.debug("Reply phase")
  65. app = create_app()
  66. with app.app_context():
  67. return self.handle_reply(envelope, smtp, msg)
  68. def handle_forward(self, envelope, smtp, msg: EmailMessage) -> str:
  69. """return *status_code message*"""
  70. alias = envelope.rcpt_tos[0] # alias@SL
  71. gen_email = GenEmail.get_by(email=alias)
  72. if not gen_email:
  73. LOG.d("alias %s not exist")
  74. return "510 Email not exist"
  75. user_email = gen_email.user.email
  76. website_email = envelope.mail_from
  77. forward_email = ForwardEmail.get_by(
  78. gen_email_id=gen_email.id, website_email=website_email
  79. )
  80. if not forward_email:
  81. LOG.debug(
  82. "create forward email for alias %s and website email %s",
  83. alias,
  84. website_email,
  85. )
  86. # todo: make sure reply_email is unique
  87. reply_email = f"reply+{random_words()}@{EMAIL_DOMAIN}"
  88. forward_email = ForwardEmail.create(
  89. gen_email_id=gen_email.id,
  90. website_email=website_email,
  91. reply_email=reply_email,
  92. )
  93. db.session.commit()
  94. forward_log = ForwardEmailLog.create(forward_id=forward_email.id)
  95. if gen_email.enabled:
  96. # add custom header
  97. add_or_replace_header(msg, "X-SimpleLogin-Type", "Forward")
  98. # remove reply-to header if present
  99. if msg["Reply-To"]:
  100. LOG.d("Delete reply-to header %s", msg["Reply-To"])
  101. del msg["Reply-To"]
  102. # change the from header so the sender comes from @SL
  103. # so it can pass DMARC check
  104. # replace the email part in from: header
  105. from_header = (
  106. get_email_name(msg["From"])
  107. + " - "
  108. + website_email.replace("@", " at ")
  109. + f" <{forward_email.reply_email}>"
  110. )
  111. msg.replace_header("From", from_header)
  112. LOG.d("new from header:%s", from_header)
  113. # change the to: header so target is user email
  114. to_header = alias.replace("@", " at ") + f" <{user_email}>"
  115. msg.replace_header("To", to_header)
  116. LOG.d("new to header: %s", to_header)
  117. # add List-Unsubscribe header
  118. unsubscribe_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}"
  119. add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
  120. add_or_replace_header(
  121. msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
  122. )
  123. original_subject = msg["Subject"]
  124. LOG.d(
  125. "Forward mail from %s to %s, subject %s, mail_options %s, rcpt_options %s ",
  126. website_email,
  127. user_email,
  128. original_subject,
  129. envelope.mail_options,
  130. envelope.rcpt_options,
  131. )
  132. smtp.send_message(
  133. msg,
  134. from_addr=envelope.mail_from,
  135. to_addrs=[user_email], # user personal email
  136. mail_options=envelope.mail_options,
  137. rcpt_options=envelope.rcpt_options,
  138. )
  139. else:
  140. LOG.d("%s is disabled, do not forward", gen_email)
  141. forward_log.blocked = True
  142. db.session.commit()
  143. return "250 Message accepted for delivery"
  144. def handle_reply(self, envelope, smtp, msg: EmailMessage) -> str:
  145. reply_email = envelope.rcpt_tos[0]
  146. forward_email = ForwardEmail.get_by(reply_email=reply_email)
  147. alias = forward_email.gen_email.email
  148. notify_admin(f"Reply phase used by user: {forward_email.gen_email.user.email} ")
  149. # email seems to come from alias
  150. msg.replace_header("From", alias)
  151. msg.replace_header("To", forward_email.website_email)
  152. # add List-Unsubscribe header
  153. unsubscribe_link = f"{URL}/dashboard/unsubscribe/{forward_email.gen_email_id}"
  154. add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
  155. add_or_replace_header(
  156. msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click"
  157. )
  158. LOG.d(
  159. "send email from %s to %s, mail_options:%s,rcpt_options:%s",
  160. alias,
  161. forward_email.website_email,
  162. envelope.mail_options,
  163. envelope.rcpt_options,
  164. )
  165. smtp.send_message(
  166. msg,
  167. from_addr=alias,
  168. to_addrs=[forward_email.website_email],
  169. mail_options=envelope.mail_options,
  170. rcpt_options=envelope.rcpt_options,
  171. )
  172. ForwardEmailLog.create(forward_id=forward_email.id, is_reply=True)
  173. db.session.commit()
  174. return "250 Message accepted for delivery"
  175. def add_or_replace_header(msg: EmailMessage, header: str, value: str):
  176. try:
  177. msg.add_header(header, value)
  178. except ValueError:
  179. # the header exists already
  180. msg.replace_header(header, value)
  181. def get_email_name(email_from):
  182. """parse email from header and return the name part
  183. First Last <ab@cd.com> -> First Last
  184. ab@cd.com -> ""
  185. """
  186. if "<" in email_from:
  187. return email_from[: email_from.find("<")].strip()
  188. return ""
  189. if __name__ == "__main__":
  190. controller = Controller(MailHandler(), hostname="0.0.0.0", port=20381)
  191. controller.start()
  192. LOG.d("Start mail controller %s %s", controller.hostname, controller.port)
  193. while True:
  194. time.sleep(10)