All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 25s
82 lines
3.2 KiB
Python
82 lines
3.2 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
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
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 == 'welcome_email':
|
|
context = {
|
|
'image_url': 'https://picsum.photos/600/200',
|
|
'message_text': 'This is a test promotional message just for you!',
|
|
'shop_link': 'https://tradhox.com/shop'
|
|
}
|
|
subject = "Welcome to Tradhox Family"
|
|
|
|
context['logo_url'] = request.build_absolute_uri('/static/mailer/images/logo.png')
|
|
|
|
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")
|
|
|
|
logger.info(f"Attempting to send '{template_name}' test email to {recipient} via {settings.EMAIL_HOST}")
|
|
msg.send(fail_silently=False)
|
|
logger.info(f"Successfully sent '{template_name}' email to {recipient}")
|
|
|
|
# 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:
|
|
logger.error(f"Failed to send '{template_name}' email to {recipient}. Error: {str(e)}", exc_info=True)
|
|
# 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')
|