Spaces:
Sleeping
Sleeping
File size: 4,790 Bytes
718f018 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import os
import base64
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
Mail, Attachment, FileContent, FileName, FileType, Disposition
)
from django.conf import settings
from datetime import datetime
# --- CONFIGURATION ---
# 1. HARDCODE YOUR KEY HERE FOR TESTING (Remove before deploying to GitHub)
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY') # <--- PASTE YOUR KEY HERE inside quotes
SENDER_EMAIL = "gamingyash54@gmail.com" # <--- MUST match the Single Sender you verified
# ---------------------
def send_html_email(subject, recipient_list, html_content, pdf_buffer=None, filename="Report.pdf"):
"""
Sends an email using SendGrid API (Bypasses Gmail SMTP).
"""
# 1. Create the email object
message = Mail(
from_email=SENDER_EMAIL,
to_emails=recipient_list,
subject=subject,
html_content=html_content
)
# 2. Attach the PDF if it exists
if pdf_buffer:
# SendGrid requires the file to be encoded in Base64 string
encoded_file = base64.b64encode(pdf_buffer.getvalue()).decode()
attachment = Attachment(
FileContent(encoded_file),
FileName(filename),
FileType('application/pdf'),
Disposition('attachment')
)
message.attachment = attachment
# 3. Send via API
try:
print(f"🚀 Sending email via SendGrid to {recipient_list}...")
sg = SendGridAPIClient(SENDGRID_API_KEY)
response = sg.send(message)
print(f"✅ SendGrid Status: {response.status_code}")
if response.status_code in [200, 201, 202]:
print("SUCCESS: Email sent!")
else:
print(f"WARNING: Unexpected status code {response.status_code}")
except Exception as e:
print(f"❌ SendGrid Failed: {str(e)}")
if hasattr(e, 'body'):
print(f"Error Body: {e.body}")
raise e # Re-raise to alert the frontend
def get_medical_email_template(patient_name, test_date, risk_level, confidence):
"""
Returns the HTML email body.
"""
# Define colors
if risk_level in ["High", "Medium"]:
color = "#e11d48" # Red
icon = "⚠️"
else:
color = "#059669" # Green
icon = "✅"
dashboard_link = "https://respirex.vercel.app" # Change to your Vercel URL later
return f"""
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; border: 1px solid #eee; border-radius: 8px;">
<div style="background-color: #0f172a; color: white; padding: 20px; text-align: center;">
<h2 style="margin:0;">RespireX Report</h2>
</div>
<div style="padding: 30px;">
<h3>Hello {patient_name},</h3>
<p>Your analysis from {test_date} is complete.</p>
<div style="background: #f8fafc; padding: 15px; border-radius: 6px; margin: 20px 0;">
<p><strong>Result:</strong> <span style="color: {color}; font-weight: bold;">{icon} {risk_level} Risk</span></p>
<p><strong>AI Confidence:</strong> {confidence}%</p>
</div>
<p>Please find the detailed PDF report attached.</p>
<div style="text-align: center; margin-top: 30px;">
<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>
</div>
</div>
</div>
"""
def send_test_result_email(user_email, prediction):
"""
Orchestrates sending the test result email.
Handles both dict (future) and tuple (current ml_engine) formats.
"""
# 1. Extract data safely
if isinstance(prediction, dict):
label = prediction.get('label', 'Analysis Complete')
confidence = prediction.get('confidence', 0)
# Estimate risk if missing
risk_level = "High" if label == "Positive" else "Low"
elif isinstance(prediction, (tuple, list)) and len(prediction) >= 3:
# Handle tuple from ml_engine.py: (result, confidence, risk_level)
label = prediction[0]
confidence = prediction[1]
risk_level = prediction[2]
else:
label = "Unknown"
confidence = 0
risk_level = "Low"
# 2. Prepare Email
subject = f"RespireX Result: {label}"
date_str = datetime.now().strftime("%B %d, %Y")
html_content = get_medical_email_template(
patient_name="Valued Patient",
test_date=date_str,
risk_level=risk_level,
confidence=confidence
)
# 3. Send
send_html_email(subject, [user_email], html_content) |