email_service.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. import config
  5. def send_email(to_email: str, subject: str, body_html: str):
  6. try:
  7. msg = MIMEMultipart("alternative")
  8. msg["Subject"] = subject
  9. msg["From"] = config.SMTP_FROM
  10. msg["To"] = to_email
  11. html_part = MIMEText(body_html, "html")
  12. msg.attach(html_part)
  13. with smtplib.SMTP(config.SMTP_HOST, config.SMTP_PORT) as server:
  14. if config.SMTP_USER and config.SMTP_PASS:
  15. server.starttls()
  16. server.login(config.SMTP_USER, config.SMTP_PASS)
  17. server.sendmail(config.SMTP_FROM, to_email, msg.as_string())
  18. return True
  19. except Exception as e:
  20. print(f"FAILED TO SEND EMAIL: {e}")
  21. return False
  22. def send_verification_email(to_email: str, token: str, lang: str = "en"):
  23. verify_url = f"{config.FRONTEND_URL}/{lang}/auth?verify_token={token}"
  24. # Simple templates based on language
  25. templates = {
  26. "en": {
  27. "subject": "Verify your Radionica 3D account",
  28. "body": f"""
  29. <h2>Welcome to Radionica 3D!</h2>
  30. <p>Please click the link below to verify your email address and activate your account:</p>
  31. <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Verify Email</a></p>
  32. <p>Or copy this link: {verify_url}</p>
  33. """
  34. },
  35. "me": {
  36. "subject": "Potvrdite vaš Radionica 3D nalog",
  37. "body": f"""
  38. <h2>Dobrodošli u Radionicu 3D!</h2>
  39. <p>Kliknite na link ispod kako biste potvrdili vašu email adresu и aktivirali nalog:</p>
  40. <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Potvrdi Email</a></p>
  41. <p>Ili kopirajte ovaj link: {verify_url}</p>
  42. """
  43. },
  44. "ru": {
  45. "subject": "Подтвердите вашу учетную запись Radionica 3D",
  46. "body": f"""
  47. <h2>Добро пожаловать в Radionica 3D!</h2>
  48. <p>Пожалуйста, нажмите на ссылку ниже, чтобы подтвердить свой email и активировать аккаунт:</p>
  49. <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Подтвердить Email</a></p>
  50. <p>Или скопируйте эту ссылку: {verify_url}</p>
  51. """
  52. },
  53. "ua": {
  54. "subject": "Підтвердіть вашу обліковий запис Radionica 3D",
  55. "body": f"""
  56. <h2>Ласкаво просимо до Radionica 3D!</h2>
  57. <p>Будь ласка, натисніть на посилання нижче, щоб підтвердити свій email та активувати акаунт:</p>
  58. <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Підтвердити Email</a></p>
  59. <p>Або скопіюйте це посилання: {verify_url}</p>
  60. """
  61. }
  62. }
  63. tpl = templates.get(lang, templates["en"])
  64. return send_email(to_email, tpl["subject"], tpl["body"])