Spaces:
Sleeping
Sleeping
Update backend/api/email_utils.py
Browse files- backend/api/email_utils.py +24 -39
backend/api/email_utils.py
CHANGED
|
@@ -9,17 +9,13 @@ from django.conf import settings
|
|
| 9 |
# --- CONFIGURATION ---
|
| 10 |
# 1. HARDCODE YOUR KEY HERE FOR TESTING (Remove before deploying to GitHub)
|
| 11 |
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
|
| 12 |
-
SENDER_EMAIL = "gamingyash54@gmail.com"
|
| 13 |
# ---------------------
|
| 14 |
|
| 15 |
def send_html_email(subject, recipient_list, html_content, pdf_buffer=None, filename="Report.pdf"):
|
| 16 |
"""
|
| 17 |
-
Sends an email using SendGrid API.
|
| 18 |
"""
|
| 19 |
-
if not SENDGRID_API_KEY:
|
| 20 |
-
print("❌ CRITICAL ERROR: SENDGRID_API_KEY is missing! Check your .env file.")
|
| 21 |
-
return
|
| 22 |
-
|
| 23 |
message = Mail(
|
| 24 |
from_email=SENDER_EMAIL,
|
| 25 |
to_emails=recipient_list,
|
|
@@ -38,13 +34,14 @@ def send_html_email(subject, recipient_list, html_content, pdf_buffer=None, file
|
|
| 38 |
message.attachment = attachment
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
print(f"🚀
|
| 42 |
sg = SendGridAPIClient(SENDGRID_API_KEY)
|
| 43 |
response = sg.send(message)
|
| 44 |
-
print(f"✅
|
| 45 |
except Exception as e:
|
| 46 |
-
print(f"❌
|
| 47 |
-
#
|
|
|
|
| 48 |
|
| 49 |
def get_medical_email_template(patient_name, test_date, risk_level, confidence):
|
| 50 |
"""
|
|
@@ -67,11 +64,14 @@ def get_medical_email_template(patient_name, test_date, risk_level, confidence):
|
|
| 67 |
<div style="padding: 30px;">
|
| 68 |
<h3>Hello {patient_name},</h3>
|
| 69 |
<p>Your analysis from {test_date} is complete.</p>
|
|
|
|
| 70 |
<div style="background: #f8fafc; padding: 15px; border-radius: 6px; margin: 20px 0;">
|
| 71 |
<p><strong>Result:</strong> <span style="color: {color}; font-weight: bold;">{icon} {risk_level} Risk</span></p>
|
| 72 |
<p><strong>AI Confidence:</strong> {confidence}%</p>
|
| 73 |
</div>
|
|
|
|
| 74 |
<p>Please find the detailed PDF report attached.</p>
|
|
|
|
| 75 |
<div style="text-align: center; margin-top: 30px;">
|
| 76 |
<a href="{dashboard_link}" style="background-color: #2563eb; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; font-weight: bold;">Login to Dashboard</a>
|
| 77 |
</div>
|
|
@@ -86,50 +86,35 @@ def send_appointment_status_email(recipient_email, patient_name, doctor_name, ap
|
|
| 86 |
subject = f"Appointment Update - Dr. {doctor_name}"
|
| 87 |
|
| 88 |
if status == 'confirmed':
|
| 89 |
-
color = "#059669"
|
| 90 |
status_text = "CONFIRMED"
|
| 91 |
-
message_body = f"Your appointment
|
| 92 |
-
if doctor_note:
|
| 93 |
-
message_body += f"<br><br><strong>Doctor's Note:</strong> <div style='background:#f0fdf4; border-left: 4px solid #059669; padding:10px; margin-top:5px;'><em>{doctor_note}</em></div>"
|
| 94 |
-
|
| 95 |
-
elif status == 'cancelled':
|
| 96 |
-
color = "#dc2626" # Red
|
| 97 |
-
status_text = "CANCELLED"
|
| 98 |
-
message_body = f"We're sorry, your appointment with Dr. {doctor_name} scheduled for {appointment_date} has been <strong>cancelled</strong>."
|
| 99 |
-
if doctor_note:
|
| 100 |
-
message_body += f"<br><br><strong>Reason:</strong> <div style='background:#fef2f2; border-left: 4px solid #dc2626; padding:10px; margin-top:5px;'><em>{doctor_note}</em></div>"
|
| 101 |
-
message_body += "<br><br>Please book a new appointment at your convenience."
|
| 102 |
-
|
| 103 |
-
elif status == 'completed':
|
| 104 |
-
color = "#2563eb" # Blue
|
| 105 |
-
status_text = "COMPLETED"
|
| 106 |
-
message_body = f"Your appointment with Dr. {doctor_name} on {appointment_date} has been marked as <strong>completed</strong>. Thank you for using RespireX."
|
| 107 |
-
|
| 108 |
else:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
message_body = f"Dr. {doctor_name} has sent an update regarding your appointment."
|
| 113 |
-
message_body += f"<br><br><strong>New Proposed Time:</strong> {appointment_date}"
|
| 114 |
if doctor_note:
|
| 115 |
-
message_body += f"<br><br><strong>Doctor's
|
| 116 |
|
| 117 |
html_content = f"""
|
| 118 |
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; border: 1px solid #eee; border-radius: 8px;">
|
| 119 |
<div style="background-color: #0f172a; color: white; padding: 20px; text-align: center;">
|
| 120 |
-
<h2 style="margin:0;">Appointment
|
| 121 |
</div>
|
| 122 |
<div style="padding: 30px;">
|
| 123 |
<h3>Hello {patient_name},</h3>
|
| 124 |
|
| 125 |
-
<div style="background: #f8fafc; padding: 15px; border-radius: 6px; margin: 20px 0; border:
|
| 126 |
<p style="margin:0 0 10px 0; color: {color}; font-weight: bold; font-size: 1.1em;">STATUS: {status_text}</p>
|
| 127 |
-
<
|
| 128 |
</div>
|
| 129 |
|
| 130 |
-
<p style="color: #64748b; font-size: 0.9em;">Please
|
| 131 |
</div>
|
| 132 |
</div>
|
| 133 |
"""
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# --- CONFIGURATION ---
|
| 10 |
# 1. HARDCODE YOUR KEY HERE FOR TESTING (Remove before deploying to GitHub)
|
| 11 |
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
|
| 12 |
+
SENDER_EMAIL = "gamingyash54@gmail.com"
|
| 13 |
# ---------------------
|
| 14 |
|
| 15 |
def send_html_email(subject, recipient_list, html_content, pdf_buffer=None, filename="Report.pdf"):
|
| 16 |
"""
|
| 17 |
+
Sends an email using SendGrid API (Bypasses Gmail SMTP).
|
| 18 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
message = Mail(
|
| 20 |
from_email=SENDER_EMAIL,
|
| 21 |
to_emails=recipient_list,
|
|
|
|
| 34 |
message.attachment = attachment
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
print(f"🚀 Sending email via SendGrid to {recipient_list}...")
|
| 38 |
sg = SendGridAPIClient(SENDGRID_API_KEY)
|
| 39 |
response = sg.send(message)
|
| 40 |
+
print(f"✅ SendGrid Status: {response.status_code}")
|
| 41 |
except Exception as e:
|
| 42 |
+
print(f"❌ SendGrid Failed: {str(e)}")
|
| 43 |
+
# Don't raise here to prevent crashing the view if email fails
|
| 44 |
+
pass
|
| 45 |
|
| 46 |
def get_medical_email_template(patient_name, test_date, risk_level, confidence):
|
| 47 |
"""
|
|
|
|
| 64 |
<div style="padding: 30px;">
|
| 65 |
<h3>Hello {patient_name},</h3>
|
| 66 |
<p>Your analysis from {test_date} is complete.</p>
|
| 67 |
+
|
| 68 |
<div style="background: #f8fafc; padding: 15px; border-radius: 6px; margin: 20px 0;">
|
| 69 |
<p><strong>Result:</strong> <span style="color: {color}; font-weight: bold;">{icon} {risk_level} Risk</span></p>
|
| 70 |
<p><strong>AI Confidence:</strong> {confidence}%</p>
|
| 71 |
</div>
|
| 72 |
+
|
| 73 |
<p>Please find the detailed PDF report attached.</p>
|
| 74 |
+
|
| 75 |
<div style="text-align: center; margin-top: 30px;">
|
| 76 |
<a href="{dashboard_link}" style="background-color: #2563eb; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; font-weight: bold;">Login to Dashboard</a>
|
| 77 |
</div>
|
|
|
|
| 86 |
subject = f"Appointment Update - Dr. {doctor_name}"
|
| 87 |
|
| 88 |
if status == 'confirmed':
|
| 89 |
+
color = "#059669" # Green
|
| 90 |
status_text = "CONFIRMED"
|
| 91 |
+
message_body = f"Your appointment has been <strong>confirmed</strong> for {appointment_date}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
else:
|
| 93 |
+
color = "#e11d48" # Red
|
| 94 |
+
status_text = "NEEDS RESCHEDULING"
|
| 95 |
+
message_body = f"Dr. {doctor_name} has sent a message regarding your appointment request for {appointment_date}."
|
|
|
|
|
|
|
| 96 |
if doctor_note:
|
| 97 |
+
message_body += f"<br><br><strong>Doctor's Note:</strong> <em style='background:#fff1f2; padding:5px;'>{doctor_note}</em>"
|
| 98 |
|
| 99 |
html_content = f"""
|
| 100 |
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; border: 1px solid #eee; border-radius: 8px;">
|
| 101 |
<div style="background-color: #0f172a; color: white; padding: 20px; text-align: center;">
|
| 102 |
+
<h2 style="margin:0;">Appointment Update</h2>
|
| 103 |
</div>
|
| 104 |
<div style="padding: 30px;">
|
| 105 |
<h3>Hello {patient_name},</h3>
|
| 106 |
|
| 107 |
+
<div style="background: #f8fafc; padding: 15px; border-radius: 6px; margin: 20px 0; border-left: 4px solid {color};">
|
| 108 |
<p style="margin:0 0 10px 0; color: {color}; font-weight: bold; font-size: 1.1em;">STATUS: {status_text}</p>
|
| 109 |
+
<p style="margin:0; line-height: 1.5;">{message_body}</p>
|
| 110 |
</div>
|
| 111 |
|
| 112 |
+
<p style="color: #64748b; font-size: 0.9em;">Please log in to your dashboard to view more details or book a new slot.</p>
|
| 113 |
</div>
|
| 114 |
</div>
|
| 115 |
"""
|
| 116 |
|
| 117 |
+
try:
|
| 118 |
+
send_html_email(subject, [recipient_email], html_content)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Failed to send appointment email: {e}")
|