email_utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import os
  2. from email.message import EmailMessage
  3. from email.utils import make_msgid, formatdate
  4. from smtplib import SMTP
  5. from jinja2 import Environment, FileSystemLoader
  6. from app.config import (
  7. SUPPORT_EMAIL,
  8. ROOT_DIR,
  9. POSTFIX_SERVER,
  10. ADMIN_EMAIL,
  11. NOT_SEND_EMAIL,
  12. )
  13. from app.log import LOG
  14. def _render(template_name, **kwargs) -> str:
  15. templates_dir = os.path.join(ROOT_DIR, "templates", "emails")
  16. env = Environment(loader=FileSystemLoader(templates_dir))
  17. template = env.get_template(template_name)
  18. return template.render(**kwargs)
  19. def send_welcome_email(email, name):
  20. send_by_postfix(
  21. email,
  22. f"{name}, welcome to SimpleLogin!",
  23. _render("welcome.txt", name=name),
  24. _render("welcome.html", name=name),
  25. )
  26. def send_activation_email(email, name, activation_link):
  27. send_by_postfix(
  28. email,
  29. f"{name}, just one more step to join SimpleLogin",
  30. _render(
  31. "activation.txt", name=name, activation_link=activation_link, email=email
  32. ),
  33. _render(
  34. "activation.html", name=name, activation_link=activation_link, email=email
  35. ),
  36. )
  37. def send_reset_password_email(email, name, reset_password_link):
  38. send_by_postfix(
  39. email,
  40. f"{name}, reset your password on SimpleLogin",
  41. _render(
  42. "reset-password.txt", name=name, reset_password_link=reset_password_link
  43. ),
  44. _render(
  45. "reset-password.html", name=name, reset_password_link=reset_password_link
  46. ),
  47. )
  48. def send_change_email(new_email, current_email, name, link):
  49. send_by_postfix(
  50. new_email,
  51. f"{name}, confirm email update on SimpleLogin",
  52. _render(
  53. "change-email.txt",
  54. name=name,
  55. link=link,
  56. new_email=new_email,
  57. current_email=current_email,
  58. ),
  59. _render(
  60. "change-email.html",
  61. name=name,
  62. link=link,
  63. new_email=new_email,
  64. current_email=current_email,
  65. ),
  66. )
  67. def send_new_app_email(email, name):
  68. send_by_postfix(
  69. email,
  70. f"{name}, any questions/feedbacks for SimpleLogin?",
  71. _render("new-app.txt", name=name),
  72. _render("new-app.html", name=name),
  73. )
  74. def send_test_email_alias(email, name):
  75. send_by_postfix(
  76. email,
  77. f"{name}, this email is sent to {email}",
  78. _render("test-email.txt", name=name, alias=email),
  79. _render("test-email.html", name=name, alias=email),
  80. )
  81. def send_by_postfix(to_email, subject, plaintext, html):
  82. if NOT_SEND_EMAIL:
  83. LOG.d(
  84. "send email with subject %s to %s, plaintext: %s, html:%s",
  85. subject,
  86. to_email,
  87. plaintext,
  88. html,
  89. )
  90. return
  91. # host IP, setup via Docker network
  92. smtp = SMTP(POSTFIX_SERVER, 25)
  93. msg = EmailMessage()
  94. msg["Subject"] = subject
  95. msg["From"] = f"Son from SimpleLogin <{SUPPORT_EMAIL}>"
  96. msg["To"] = to_email
  97. msg.set_content(plaintext)
  98. if html is not None:
  99. msg.add_alternative(html, subtype="html")
  100. msg_id_header = make_msgid()
  101. LOG.d("message-id %s", msg_id_header)
  102. msg["Message-ID"] = msg_id_header
  103. date_header = formatdate()
  104. LOG.d("Date header: %s", date_header)
  105. msg["Date"] = date_header
  106. smtp.send_message(msg, from_addr=SUPPORT_EMAIL, to_addrs=[to_email])
  107. def notify_admin(subject, html_content=""):
  108. send_by_postfix(ADMIN_EMAIL, subject, html_content, html_content)