From 480f9113ff7c804b263585d19bddf1b935369ed3 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Mon, 24 Aug 2026 11:07:20 +0330 Subject: [PATCH] otp updated for dovodi --- config/settings/base.py | 6 ++--- utils/__init__.py | 53 +++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index cb76f4b..4f62f3c 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -105,10 +105,8 @@ REDIS_URL = env('REDIS_URL') -OTP_SERIVCE_KEY = "33213d78f1234e99b81f94eefda77e45" - -RESEND_API_KEY = "re_JFFAfESy_JHmJTJLu5ToGTPhwrZx7trKd" -RESEND_FROM_EMAIL = "info@imamjavad.online" +RESEND_API_KEY = env('RESEND_API_KEY', default="re_JFFAfESy_JHmJTJLu5ToGTPhwrZx7trKd") +RESEND_FROM_EMAIL = env('RESEND_FROM_EMAIL', default="Dovodi ") PHONENUMBER_DEFAULT_REGION = "IR" diff --git a/utils/__init__.py b/utils/__init__.py index 12b998c..bd0eda0 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -90,22 +90,38 @@ def environment_callback(request): return [_("Production"), "primary"] - def send_email(recipient, code): import requests from django.conf import settings - if not getattr(settings, "RESEND_API_KEY", None): + api_key = getattr(settings, "RESEND_API_KEY", None) + if not api_key: logger.warning("RESEND_API_KEY is not set in settings.") return False url = "https://api.resend.com/emails" headers = { - "Authorization": f"Bearer {settings.RESEND_API_KEY}", + "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } - site_domain = getattr(settings, "SITE_DOMAIN", "https://dovodi.newhorizonco.uk/") + # Normalize recipient into a clean list of email strings + if isinstance(recipient, str): + to_emails = [recipient.strip()] + elif isinstance(recipient, (list, tuple, set)): + to_emails = [str(r).strip() for r in recipient if r and str(r).strip()] + else: + to_emails = [str(recipient).strip()] + + if not to_emails: + logger.error(f"Cannot send email: recipient list is empty ({recipient})") + return False + + from_email = getattr(settings, "RESEND_FROM_EMAIL", "Dovodi ") + if "<" not in from_email and "@" in from_email: + from_email = f"Dovodi <{from_email}>" + + site_domain = getattr(settings, "SITE_DOMAIN", "https://dovodi.newhorizonco.uk/").rstrip('/') subject = "Код подтверждения | Verification Code" html_content = f""" @@ -128,7 +144,7 @@ def send_email(recipient, code): - Dovodi Logo + Dovodi @@ -148,7 +164,7 @@ def send_email(recipient, code):

- Используйте следующий одноразовый код для входа или подтверждения учетной записи в Dovodi: + Используйте следующий одноразовый код для входа یا подтверждения учетной записи в Dovodi:

@@ -183,7 +199,7 @@ def send_email(recipient, code):

dovodi.newhorizonco.uk  •  - [EMAIL_ADDRESS] + info@imamjavad.online

© 2026 Dovodi. Все права защищены. @@ -197,20 +213,33 @@ def send_email(recipient, code): """ + + text_content = ( + f"Код подтверждения Dovodi: {code}\n\n" + f"Your Dovodi verification code is: {code}\n\n" + f"Срок действия кода истекает через 5 минут. Если вы не запрашивали этот код, проигнорируйте это письмо.\n" + f"Никому не передавайте этот код в целях безопасности вашего аккаунта." + ) payload = { - "from": settings.RESEND_FROM_EMAIL, - "to": recipient, + "from": from_email, + "to": to_emails, "subject": subject, "html": html_content, + "text": text_content, } try: - response = requests.post(url, headers=headers, json=payload) - response.raise_for_status() + response = requests.post(url, headers=headers, json=payload, timeout=15) + if response.status_code >= 400: + logger.error(f"Failed to send email via Resend: HTTP {response.status_code} - {response.text}") + print(f"Failed to send email via Resend: HTTP {response.status_code} - {response.text}") + return False + logger.info(f"Email OTP sent successfully via Resend to {to_emails}") return True except Exception as e: - logger.error(f"Failed to send email via Resend: {str(e)}") + logger.error(f"Failed to send email via Resend: {str(e)}", exc_info=True) + print(f"Failed to send email via Resend: {str(e)}") return False