diff --git a/mailer/api.py b/mailer/api.py new file mode 100644 index 0000000..56ec837 --- /dev/null +++ b/mailer/api.py @@ -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) diff --git a/mailer/templates/emails/sellercentral/common/verification.html b/mailer/templates/emails/sellercentral/common/verification.html index d20fd25..8bb5c8d 100644 --- a/mailer/templates/emails/sellercentral/common/verification.html +++ b/mailer/templates/emails/sellercentral/common/verification.html @@ -32,7 +32,7 @@
Your Verification Code