From d670baf594f114d10db47f45846961075d521a84 Mon Sep 17 00:00:00 2001 From: saani Date: Sun, 23 Nov 2025 00:27:44 +0000 Subject: [PATCH] refactor(email): improve email service implementation and templates - Remove strip_tags usage and use explicit fallback text for HTML emails - Use named parameters in EmailMultiAlternatives for better clarity - Add fail_silently=False to email.send() for explicit error handling - Rename variables (html_content -> html_message, email_msg -> email) - Remove action buttons from appointment email templates These changes improve code readability and provide a clearer fallback message for non-HTML email clients instead of relying on stripped HTML. --- meetings/email_service.py | 52 ++++++++++----------- templates/emails/appointment_rejected.html | 7 --- templates/emails/appointment_scheduled.html | 32 ------------- 3 files changed, 24 insertions(+), 67 deletions(-) diff --git a/meetings/email_service.py b/meetings/email_service.py index c1067fa..4fb0b1a 100644 --- a/meetings/email_service.py +++ b/meetings/email_service.py @@ -1,6 +1,5 @@ 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 class EmailService: @@ -16,20 +15,19 @@ class EmailService: 'admin_dashboard_url': f"{settings.FRONTEND_URL}/admin/appointments" if hasattr(settings, 'FRONTEND_URL') else '/admin/' } - html_content = render_to_string('emails/admin_notification.html', context) - text_content = strip_tags(html_content) + html_message = render_to_string('emails/admin_notification.html', context) admin_email = getattr(settings, 'ADMIN_EMAIL', 'hello@attunehearttherapy.com') try: - email_msg = EmailMultiAlternatives( - subject, - text_content, - settings.DEFAULT_FROM_EMAIL, - [admin_email] + email = EmailMultiAlternatives( + subject=subject, + body="Please view this email in an HTML-compatible client.", # Fallback text + from_email=settings.DEFAULT_FROM_EMAIL, + to=[admin_email], ) - email_msg.attach_alternative(html_content, "text/html") - email_msg.send() + email.attach_alternative(html_message, "text/html") + email.send(fail_silently=False) return True except Exception as e: print(f"Failed to send admin notification: {e}") @@ -45,18 +43,17 @@ class EmailService: 'user_dashboard_url': f"{settings.FRONTEND_URL}/dashboard" if hasattr(settings, 'FRONTEND_URL') else '/dashboard/' } - html_content = render_to_string('emails/appointment_scheduled.html', context) - text_content = strip_tags(html_content) + html_message = render_to_string('emails/appointment_scheduled.html', context) try: - email_msg = EmailMultiAlternatives( - subject, - text_content, - settings.DEFAULT_FROM_EMAIL, - [appointment.email] + email = EmailMultiAlternatives( + subject=subject, + body="Please view this email in an HTML-compatible client.", # Fallback text + from_email=settings.DEFAULT_FROM_EMAIL, + to=[appointment.email], ) - email_msg.attach_alternative(html_content, "text/html") - email_msg.send() + email.attach_alternative(html_message, "text/html") + email.send(fail_silently=False) return True except Exception as e: print(f"Failed to send scheduled notification: {e}") @@ -72,18 +69,17 @@ class EmailService: 'user_dashboard_url': f"{settings.FRONTEND_URL}/dashboard" if hasattr(settings, 'FRONTEND_URL') else '/dashboard/' } - html_content = render_to_string('emails/appointment_rejected.html', context) - text_content = strip_tags(html_content) + html_message = render_to_string('emails/appointment_rejected.html', context) try: - email_msg = EmailMultiAlternatives( - subject, - text_content, - settings.DEFAULT_FROM_EMAIL, - [appointment.email] + email = EmailMultiAlternatives( + subject=subject, + body="Please view this email in an HTML-compatible client.", # Fallback text + from_email=settings.DEFAULT_FROM_EMAIL, + to=[appointment.email], ) - email_msg.attach_alternative(html_content, "text/html") - email_msg.send() + email.attach_alternative(html_message, "text/html") + email.send(fail_silently=False) return True except Exception as e: print(f"Failed to send rejection notification: {e}") diff --git a/templates/emails/appointment_rejected.html b/templates/emails/appointment_rejected.html index 87b43d9..8197d75 100644 --- a/templates/emails/appointment_rejected.html +++ b/templates/emails/appointment_rejected.html @@ -287,13 +287,6 @@
  • Check back next month for updated availability
  • - - -
    - - Submit New Request - -
    diff --git a/templates/emails/appointment_scheduled.html b/templates/emails/appointment_scheduled.html index 4a06dee..f5eb747 100644 --- a/templates/emails/appointment_scheduled.html +++ b/templates/emails/appointment_scheduled.html @@ -266,38 +266,6 @@
    With: Nathalie (Therapist)
    -
    -
    -
    -
    Duration
    -
    - {{ appointment.meeting_duration_display }} -
    -
    -
    -
    Session Type
    -
    Virtual Meeting
    -
    -
    - - -
    -
    📋 To Prepare for Your Session
    - -
    - - -
    - - View in Dashboard - -