12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import os
- from email.message import EmailMessage
- from smtplib import SMTP
- from jinja2 import Environment, FileSystemLoader
- from app.config import SUPPORT_EMAIL, ROOT_DIR
- def _render(template_name, **kwargs) -> str:
- templates_dir = os.path.join(ROOT_DIR, "templates", "emails")
- env = Environment(loader=FileSystemLoader(templates_dir))
- template = env.get_template(template_name)
- return template.render(**kwargs)
- def send_welcome_email(email, name):
- send_by_postfix(
- email,
- f"{name}, welcome to SimpleLogin!",
- _render("welcome.txt", name=name),
- _render("welcome.html", name=name),
- )
- def send_activation_email(email, name, activation_link):
- send_by_postfix(
- email,
- f"{name}, just one more step to join SimpleLogin",
- _render("activation.txt", name=name, activation_link=activation_link),
- _render("activation.html", name=name, activation_link=activation_link),
- )
- def send_reset_password_email(email, name, reset_password_link):
- send_by_postfix(
- email,
- f"{name}, reset your password on SimpleLogin",
- _render(
- "reset-password.txt", name=name, reset_password_link=reset_password_link
- ),
- _render(
- "reset-password.html", name=name, reset_password_link=reset_password_link
- ),
- )
- def send_new_app_email(email, name):
- send_by_postfix(
- email,
- f"{name}, any questions/feedbacks for SimpleLogin?",
- _render("new-app.txt", name=name),
- _render("new-app.html", name=name),
- )
- def send_by_postfix(to_email, subject, plaintext, html):
- # host IP, setup via Docker network
- smtp = SMTP("1.1.1.1", 25)
- msg = EmailMessage()
- msg["Subject"] = subject
- msg["From"] = f"Son from SimpleLogin <{SUPPORT_EMAIL}>"
- msg["To"] = to_email
- msg.set_content(plaintext)
- if html is not None:
- msg.add_alternative(html, subtype="html")
- smtp.send_message(msg, from_addr=SUPPORT_EMAIL, to_addrs=[to_email])
- def notify_admin(subject, html_content=""):
- send_by_postfix(SUPPORT_EMAIL, subject, html_content, html_content)
|