feat: add email sending API endpoint and dynamic OTP in verification template
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 24s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 24s
This commit is contained in:
parent
236c69fbc8
commit
35e1e09401
3 changed files with 76 additions and 1 deletions
73
mailer/api.py
Normal file
73
mailer/api.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.html import strip_tags
|
||||
from django.conf import settings
|
||||
from .models import EmailLog
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class SendEmailAPIView(APIView):
|
||||
def post(self, request):
|
||||
recipient = request.data.get('recipient')
|
||||
template_name = request.data.get('template_name')
|
||||
context = request.data.get('context', {})
|
||||
subject = request.data.get('subject')
|
||||
|
||||
if not recipient or not template_name:
|
||||
return Response({"error": "Recipient and template_name are required."}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Default subjects based on template if not provided
|
||||
if not subject:
|
||||
if template_name == 'verification':
|
||||
subject = "Verify Your Email"
|
||||
elif template_name == 'password_reset':
|
||||
subject = "Password Reset Request"
|
||||
elif template_name == 'welcome_email':
|
||||
subject = "Welcome to Tradhox Family"
|
||||
else:
|
||||
subject = f"Notification from Tradhox"
|
||||
|
||||
# Ensure logo_url is present
|
||||
if 'logo_url' not in context:
|
||||
context['logo_url'] = request.build_absolute_uri('/static/mailer/images/logo.png')
|
||||
|
||||
try:
|
||||
# We assume templates are in emails/sellercentral/common/
|
||||
template_path = f'emails/sellercentral/common/{template_name}.html'
|
||||
html_content = render_to_string(template_path, context)
|
||||
text_content = strip_tags(html_content)
|
||||
|
||||
msg = EmailMultiAlternatives(
|
||||
subject,
|
||||
text_content,
|
||||
settings.EMAIL_HOST_USER,
|
||||
[recipient]
|
||||
)
|
||||
msg.attach_alternative(html_content, "text/html")
|
||||
|
||||
logger.info(f"API: Attempting to send '{template_name}' email to {recipient} via {getattr(settings, 'EMAIL_HOST', 'unknown')}")
|
||||
msg.send(fail_silently=False)
|
||||
logger.info(f"API: Successfully sent '{template_name}' email to {recipient}")
|
||||
|
||||
EmailLog.objects.create(
|
||||
recipient=recipient,
|
||||
subject=subject,
|
||||
template_name=template_name,
|
||||
status='success'
|
||||
)
|
||||
return Response({"message": "Email sent successfully"}, status=status.HTTP_200_OK)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"API: Failed to send '{template_name}' email to {recipient}. Error: {str(e)}", exc_info=True)
|
||||
EmailLog.objects.create(
|
||||
recipient=recipient,
|
||||
subject=subject,
|
||||
template_name=template_name,
|
||||
status='failed',
|
||||
error_message=str(e)
|
||||
)
|
||||
return Response({"error": f"Failed to send email: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
|
@ -32,7 +32,7 @@
|
|||
<div style="background-color: #fbf9f8; border: 1px solid #d9c5b2; border-radius: 4px; padding: 24px; margin-bottom: 32px;">
|
||||
<p style="margin: 0 0 16px 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: #636260; font-weight: 600;">Your Verification Code</p>
|
||||
<div style="font-family: 'Libre Caslon Text', serif; font-size: 40px; letter-spacing: 8px; font-weight: 700; color: #6b1a2c; margin-bottom: 24px;">
|
||||
7 4 9 2 0 1
|
||||
{{ otp }}
|
||||
</div>
|
||||
<table width="100%" border="0" cellspacing="0" cellpadding="0">
|
||||
<tr>
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
from django.urls import path
|
||||
from . import views
|
||||
from . import api
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.dashboard, name='dashboard'),
|
||||
path('test-email/', views.send_test_email, name='send_test_email'),
|
||||
path('logs/', views.view_logs, name='view_logs'),
|
||||
path('api/send/', api.SendEmailAPIView.as_view(), name='api_send_email'),
|
||||
]
|
||||
|
|
|
|||
Loading…
Reference in a new issue