| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import smtplib
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- import config
- def send_email(to_email: str, subject: str, body_html: str):
- try:
- msg = MIMEMultipart("alternative")
- msg["Subject"] = subject
- msg["From"] = config.SMTP_FROM
- msg["To"] = to_email
- html_part = MIMEText(body_html, "html")
- msg.attach(html_part)
- with smtplib.SMTP(config.SMTP_HOST, config.SMTP_PORT) as server:
- if config.SMTP_USER and config.SMTP_PASS:
- server.starttls()
- server.login(config.SMTP_USER, config.SMTP_PASS)
- server.sendmail(config.SMTP_FROM, to_email, msg.as_string())
- return True
- except Exception as e:
- print(f"FAILED TO SEND EMAIL: {e}")
- return False
- def send_verification_email(to_email: str, token: str, lang: str = "en"):
- verify_url = f"{config.FRONTEND_URL}/{lang}/auth?verify_token={token}"
-
- templates = {
- "en": {
- "subject": "Verify your Radionica 3D account",
- "body": f"""
- <h2>Welcome to Radionica 3D!</h2>
- <p>Please click the link below to verify your email address and activate your account:</p>
- <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Verify Email</a></p>
- <p>Or copy this link: {verify_url}</p>
- """
- },
- "me": {
- "subject": "Potvrdite vaš Radionica 3D nalog",
- "body": f"""
- <h2>Dobrodošli u Radionicu 3D!</h2>
- <p>Kliknite na link ispod kako biste potvrdili vašu email adresu и aktivirali nalog:</p>
- <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Potvrdi Email</a></p>
- <p>Ili kopirajte ovaj link: {verify_url}</p>
- """
- },
- "ru": {
- "subject": "Подтвердите вашу учетную запись Radionica 3D",
- "body": f"""
- <h2>Добро пожаловать в Radionica 3D!</h2>
- <p>Пожалуйста, нажмите на ссылку ниже, чтобы подтвердить свой email и активировать аккаунт:</p>
- <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Подтвердить Email</a></p>
- <p>Или скопируйте эту ссылку: {verify_url}</p>
- """
- },
- "ua": {
- "subject": "Підтвердіть вашу обліковий запис Radionica 3D",
- "body": f"""
- <h2>Ласкаво просимо до Radionica 3D!</h2>
- <p>Будь ласка, натисніть на посилання нижче, щоб підтвердити свій email та активувати акаунт:</p>
- <p><a href="{verify_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Підтвердити Email</a></p>
- <p>Або скопіюйте це посилання: {verify_url}</p>
- """
- }
- }
-
- tpl = templates.get(lang, templates["en"])
- return send_email(to_email, tpl["subject"], tpl["body"])
- def send_password_reset_email(to_email: str, token: str, lang: str = "en"):
- reset_url = f"{config.FRONTEND_URL}/{lang}/auth?token={token}"
-
- templates = {
- "en": {
- "subject": "Reset your Radionica 3D password",
- "body": f"""
- <h2>Password Reset Request</h2>
- <p>You requested a password reset. Click the button below to set a new password. This link is valid for 10 minutes.</p>
- <p><a href="{reset_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Reset Password</a></p>
- <p>Or copy this link: {reset_url}</p>
- """
- },
- "me": {
- "subject": "Resetovanje lozinke na Radionica 3D",
- "body": f"""
- <h2>Zahtjev za resetovanje lozinke</h2>
- <p>Zatražili ste resetovanje lozinke. Kliknite na dugme ispod kako biste postavili novu lozinku. Ovaj link važi 10 minuta.</p>
- <p><a href="{reset_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Resetuj lozinku</a></p>
- <p>Ili kopirajte ovaj link: {reset_url}</p>
- """
- },
- "ru": {
- "subject": "Сброс пароля Radionica 3D",
- "body": f"""
- <h2>Запрос на сброс пароля</h2>
- <p>Вы запросили сброс пароля. Нажмите на кнопку ниже, чтобы установить новый пароль. Ссылка действительна в течение 10 минут.</p>
- <p><a href="{reset_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Сбросить пароль</a></p>
- <p>Или скопируйте эту ссылку: {reset_url}</p>
- """
- },
- "ua": {
- "subject": "Скидання пароля Radionica 3D",
- "body": f"""
- <h2>Запит на скидання пароля</h2>
- <p>Ви запросили скидання пароля. Натисніть на кнопку нижче, щоб встановити новий пароль. Посилання дійсне протягом 10 хвилин.</p>
- <p><a href="{reset_url}" style="padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none; border-radius: 5px;">Скинути пароль</a></p>
- <p>Або скопіюйте це посилання: {reset_url}</p>
- """
- }
- }
-
- tpl = templates.get(lang, templates["en"])
- return send_email(to_email, tpl["subject"], tpl["body"])
- def send_contact_form_email(admin_email: str, name: str, from_email: str, subject: str, message: str):
- """Notify admin about new contact form submission"""
- subject_line = f"Contact Form: {subject} (from {name})"
- body = f"""
- <h2 style="color: #000;">New Contact Form Submission</h2>
- <hr>
- <p><strong>From:</strong> {name} ({from_email})</p>
- <p><strong>Subject:</strong> {subject}</p>
- <p><strong>Message:</strong></p>
- <div style="padding: 15px; background-color: #f9f9f9; border-left: 4px solid #000; white-space: pre-wrap;">
- {message}
- </div>
- <hr>
- <p style="font-size: 12px; color: #666;">This message was sent from the Radionica 3D Contact page.</p>
- """
- return send_email(admin_email, subject_line, body)
|