diff --git a/apps/account/tasks.py b/apps/account/tasks.py index aafcdab..abf14b4 100644 --- a/apps/account/tasks.py +++ b/apps/account/tasks.py @@ -158,3 +158,35 @@ def send_otp_code_whatsapp(phone_number, code): time.sleep(2) +@shared_task +def send_resend_email_task(recipient_list, subject, html_content): + """ + Sends an email using Resend API. + """ + if not settings.RESEND_API_KEY: + logger.error("RESEND_API_KEY is not set in settings.") + return False + + url = "https://api.resend.com/emails" + headers = { + "Authorization": f"Bearer {settings.RESEND_API_KEY}", + "Content-Type": "application/json", + } + + payload = { + "from": settings.RESEND_FROM_EMAIL, + "to": recipient_list, + "subject": subject, + "html": html_content, + } + + try: + response = requests.post(url, headers=headers, json=payload) + response.raise_for_status() + logger.info(f"Email sent successfully to {recipient_list} via Resend.") + return response.json() + except Exception as e: + logger.error(f"Failed to send email via Resend: {str(e)}") + return False + + diff --git a/config/settings/base.py b/config/settings/base.py index 3dd3f1f..1b14a57 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -112,6 +112,9 @@ REDIS_URL = env('REDIS_URL') OTP_SERIVCE_KEY = "33213d78f1234e99b81f94eefda77e45" +RESEND_API_KEY = "re_JFFAfESy_JHmJTJLu5ToGTPhwrZx7trKd" +RESEND_FROM_EMAIL = "info@imamjavad.online" + PHONENUMBER_DEFAULT_REGION = "IR" PHONENUMBER_DB_FORMAT = 'INTERNATIONAL' diff --git a/utils/__init__.py b/utils/__init__.py index 4ad546c..a5f6257 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -82,13 +82,22 @@ def environment_callback(request): def send_email(recipient, code): - send_mail( - 'Test Email', - f'This is a test email {code} from Django using Gmail SMTP.', - 'aliabdolahi.171@gmail.com', - recipient, - fail_silently=False, - ) + from apps.account.tasks import send_resend_email_task + subject = 'Verification Code' + html_content = f""" +
+

Verification Code

+

Hello,

+

Your verification code for Imam Javad App is:

+
+ {code} +
+

This code will expire shortly. If you did not request this code, please ignore this email.

+
+

Sent via Resend API

+
+ """ + send_resend_email_task.delay(recipient, subject, html_content) return True