emailservice/mailer/views.py
vickytechkey 663e3cdbd7
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 45s
Initial commit
2026-08-23 09:28:40 +05:30

73 lines
2.7 KiB
Python

from django.shortcuts import render, redirect
from django.contrib import messages
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
def dashboard(request):
return render(request, 'mailer/dashboard.html')
def send_test_email(request):
if request.method == 'POST':
recipient = request.POST.get('recipient')
template_name = request.POST.get('template_name')
if not recipient or not template_name:
messages.error(request, "Recipient and template are required.")
return redirect('send_test_email')
subject = f"Test Email: {template_name}"
context = {}
# Mock data for templates
if template_name == 'verification':
context = {'verification_link': 'https://tradhox.com/verify/mock-token'}
subject = "Verify Your Email"
elif template_name == 'password_reset':
context = {'reset_link': 'https://tradhox.com/reset/mock-token'}
subject = "Password Reset Request"
elif template_name == 'promotional':
context = {
'image_url': 'https://via.placeholder.com/600x200',
'message_text': 'This is a test promotional message just for you!',
'shop_link': 'https://tradhox.com/shop'
}
subject = "Welcome to Tradhox Family"
try:
html_content = render_to_string(f'emails/sellercentral/common/{template_name}.html', context)
text_content = strip_tags(html_content)
msg = EmailMultiAlternatives(
subject,
text_content,
settings.EMAIL_HOST_USER,
[recipient]
)
msg.attach_alternative(html_content, "text/html")
msg.send(fail_silently=False)
# Log success
EmailLog.objects.create(
recipient=recipient,
subject=subject,
template_name=template_name,
status='success'
)
messages.success(request, f"Test email sent successfully to {recipient}!")
except Exception as e:
# Log failure
EmailLog.objects.create(
recipient=recipient,
subject=subject,
template_name=template_name,
status='failed',
error_message=str(e)
)
messages.error(request, f"Failed to send email: {e}")
return redirect('dashboard')
return render(request, 'mailer/send_test.html')