Browse Source

email handles without celery

master
Mohsen Taba 1 week ago
parent
commit
4c3e0991a2
  1. 31
      apps/account/tasks.py
  2. 29
      utils/__init__.py

31
apps/account/tasks.py

@ -158,35 +158,4 @@ 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

29
utils/__init__.py

@ -82,7 +82,19 @@ def environment_callback(request):
def send_email(recipient, code):
from apps.account.tasks import send_resend_email_task
import requests
from django.conf import settings
if not settings.RESEND_API_KEY:
print("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",
}
subject = 'Verification Code'
html_content = f"""
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #e0e0e0; border-radius: 10px;">
@ -97,8 +109,21 @@ def send_email(recipient, code):
<p style="font-size: 12px; color: #999; text-align: center;">Sent via Resend API</p>
</div>
"""
send_resend_email_task.delay(recipient, subject, html_content)
payload = {
"from": settings.RESEND_FROM_EMAIL,
"to": recipient,
"subject": subject,
"html": html_content,
}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return True
except Exception as e:
print(f"Failed to send email via Resend: {str(e)}")
return False
def is_valid_email(email):

Loading…
Cancel
Save