Browse Source

email service set up

master
Mohsen Taba 1 week ago
parent
commit
2968e6e98c
  1. 32
      apps/account/tasks.py
  2. 3
      config/settings/base.py
  3. 23
      utils/__init__.py

32
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

3
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'

23
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"""
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #e0e0e0; border-radius: 10px;">
<h2 style="color: #333; text-align: center;">Verification Code</h2>
<p style="font-size: 16px; color: #555;">Hello,</p>
<p style="font-size: 16px; color: #555;">Your verification code for <strong>Imam Javad App</strong> is:</p>
<div style="text-align: center; margin: 30px 0;">
<span style="font-size: 32px; font-weight: bold; color: #007bff; letter-spacing: 5px; background: #f8f9fa; padding: 10px 20px; border-radius: 5px; border: 1px dashed #007bff;">{code}</span>
</div>
<p style="font-size: 14px; color: #777; text-align: center;">This code will expire shortly. If you did not request this code, please ignore this email.</p>
<hr style="border: 0; border-top: 1px solid #eee; margin: 20px 0;">
<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)
return True

Loading…
Cancel
Save