2025-11-23 00:19:26 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
class EmailService:
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def send_admin_notification(appointment):
|
|
|
|
|
subject = f"New Appointment Request from {appointment.full_name}"
|
|
|
|
|
|
|
|
|
|
context = {
|
|
|
|
|
'appointment': appointment,
|
|
|
|
|
'preferred_dates': appointment.get_preferred_dates_display(),
|
|
|
|
|
'preferred_times': appointment.get_preferred_time_slots_display(),
|
|
|
|
|
'admin_dashboard_url': f"{settings.FRONTEND_URL}/admin/appointments" if hasattr(settings, 'FRONTEND_URL') else '/admin/'
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 00:27:44 +00:00
|
|
|
html_message = render_to_string('emails/admin_notification.html', context)
|
2025-11-23 00:19:26 +00:00
|
|
|
|
|
|
|
|
admin_email = getattr(settings, 'ADMIN_EMAIL', 'hello@attunehearttherapy.com')
|
|
|
|
|
|
|
|
|
|
try:
|
2025-11-23 00:27:44 +00:00
|
|
|
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],
|
2025-11-23 00:19:26 +00:00
|
|
|
)
|
2025-11-23 00:27:44 +00:00
|
|
|
email.attach_alternative(html_message, "text/html")
|
|
|
|
|
email.send(fail_silently=False)
|
2025-11-23 00:19:26 +00:00
|
|
|
return True
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Failed to send admin notification: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def send_appointment_scheduled(appointment):
|
|
|
|
|
subject = "Your Appointment Has Been Scheduled"
|
|
|
|
|
|
|
|
|
|
context = {
|
|
|
|
|
'appointment': appointment,
|
|
|
|
|
'scheduled_datetime': appointment.formatted_scheduled_datetime,
|
|
|
|
|
'user_dashboard_url': f"{settings.FRONTEND_URL}/dashboard" if hasattr(settings, 'FRONTEND_URL') else '/dashboard/'
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 00:27:44 +00:00
|
|
|
html_message = render_to_string('emails/appointment_scheduled.html', context)
|
2025-11-23 00:19:26 +00:00
|
|
|
|
|
|
|
|
try:
|
2025-11-23 00:27:44 +00:00
|
|
|
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],
|
2025-11-23 00:19:26 +00:00
|
|
|
)
|
2025-11-23 00:27:44 +00:00
|
|
|
email.attach_alternative(html_message, "text/html")
|
|
|
|
|
email.send(fail_silently=False)
|
2025-11-23 00:19:26 +00:00
|
|
|
return True
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Failed to send scheduled notification: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def send_appointment_rejected(appointment):
|
|
|
|
|
subject = "Update on Your Appointment Request"
|
|
|
|
|
|
|
|
|
|
context = {
|
|
|
|
|
'appointment': appointment,
|
|
|
|
|
'rejection_reason': appointment.rejection_reason or "No specific reason provided.",
|
|
|
|
|
'user_dashboard_url': f"{settings.FRONTEND_URL}/dashboard" if hasattr(settings, 'FRONTEND_URL') else '/dashboard/'
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 00:27:44 +00:00
|
|
|
html_message = render_to_string('emails/appointment_rejected.html', context)
|
2025-11-23 00:19:26 +00:00
|
|
|
|
|
|
|
|
try:
|
2025-11-23 00:27:44 +00:00
|
|
|
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],
|
2025-11-23 00:19:26 +00:00
|
|
|
)
|
2025-11-23 00:27:44 +00:00
|
|
|
email.attach_alternative(html_message, "text/html")
|
|
|
|
|
email.send(fail_silently=False)
|
2025-11-23 00:19:26 +00:00
|
|
|
return True
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Failed to send rejection notification: {e}")
|
|
|
|
|
return False
|