Update src/sendgrid_service.py
Browse files- src/sendgrid_service.py +16 -178
src/sendgrid_service.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 2 |
import base64
|
| 3 |
import logging
|
| 4 |
import requests
|
| 5 |
-
from datetime import datetime
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -12,12 +11,11 @@ class SendGridService:
|
|
| 12 |
def __init__(self):
|
| 13 |
self.api_key = os.getenv('SENDGRID_API_KEY')
|
| 14 |
self.from_email = os.getenv('SENDGRID_FROM_EMAIL', 'noreply@daease.com')
|
| 15 |
-
self.from_name = "Daease Medical Assistant"
|
| 16 |
|
| 17 |
if not self.api_key:
|
| 18 |
logger.warning("SendGrid API key not found in environment variables")
|
| 19 |
|
| 20 |
-
def send_report(self, to_email, pdf_data, patient_name
|
| 21 |
"""
|
| 22 |
Send medical report via SendGrid's API
|
| 23 |
|
|
@@ -25,7 +23,6 @@ class SendGridService:
|
|
| 25 |
to_email (str): Recipient's email address
|
| 26 |
pdf_data (bytes): The PDF report as bytes
|
| 27 |
patient_name (str): Name of the patient
|
| 28 |
-
conversation_summary (str, optional): Summary of the medical conversation
|
| 29 |
|
| 30 |
Returns:
|
| 31 |
tuple[bool, str]: (success status, message)
|
|
@@ -42,147 +39,29 @@ class SendGridService:
|
|
| 42 |
safe_name = patient_name.replace(' ', '_').replace(',', '').replace('.', '')
|
| 43 |
pdf_filename = f"medical_report_daease_{safe_name}.pdf"
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
current_date = datetime.now().strftime("%B %d, %Y")
|
| 47 |
-
|
| 48 |
-
# Create a summary if none provided
|
| 49 |
-
if not conversation_summary:
|
| 50 |
-
conversation_summary = "A consultation was conducted with our AI Medical Assistant, covering your medical concerns. The detailed findings and recommendations are included in the attached report."
|
| 51 |
-
|
| 52 |
-
# HTML version of the email with logo and better formatting
|
| 53 |
-
html_content = f"""
|
| 54 |
-
<!DOCTYPE html>
|
| 55 |
-
<html>
|
| 56 |
-
<head>
|
| 57 |
-
<meta charset="UTF-8">
|
| 58 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 59 |
-
<title>Daease Medical Report</title>
|
| 60 |
-
<style>
|
| 61 |
-
body {{
|
| 62 |
-
font-family: Arial, sans-serif;
|
| 63 |
-
line-height: 1.6;
|
| 64 |
-
color: #333;
|
| 65 |
-
margin: 0;
|
| 66 |
-
padding: 0;
|
| 67 |
-
background-color: #f9f9f9;
|
| 68 |
-
}}
|
| 69 |
-
.container {{
|
| 70 |
-
max-width: 600px;
|
| 71 |
-
margin: 0 auto;
|
| 72 |
-
padding: 20px;
|
| 73 |
-
background-color: #ffffff;
|
| 74 |
-
}}
|
| 75 |
-
.header {{
|
| 76 |
-
text-align: center;
|
| 77 |
-
padding: 20px 0;
|
| 78 |
-
border-bottom: 1px solid #ddd;
|
| 79 |
-
}}
|
| 80 |
-
.logo {{
|
| 81 |
-
max-width: 150px;
|
| 82 |
-
height: auto;
|
| 83 |
-
}}
|
| 84 |
-
.content {{
|
| 85 |
-
padding: 20px 0;
|
| 86 |
-
}}
|
| 87 |
-
.summary {{
|
| 88 |
-
background-color: #f6f7f9;
|
| 89 |
-
padding: 15px;
|
| 90 |
-
border-radius: 5px;
|
| 91 |
-
margin: 15px 0;
|
| 92 |
-
}}
|
| 93 |
-
.footer {{
|
| 94 |
-
text-align: center;
|
| 95 |
-
padding: 20px 0;
|
| 96 |
-
font-size: 12px;
|
| 97 |
-
color: #777;
|
| 98 |
-
border-top: 1px solid #ddd;
|
| 99 |
-
}}
|
| 100 |
-
a {{
|
| 101 |
-
color: #8A2BE2;
|
| 102 |
-
text-decoration: none;
|
| 103 |
-
}}
|
| 104 |
-
.purple-text {{
|
| 105 |
-
color: #8A2BE2;
|
| 106 |
-
}}
|
| 107 |
-
</style>
|
| 108 |
-
</head>
|
| 109 |
-
<body>
|
| 110 |
-
<div class="container">
|
| 111 |
-
<div class="header">
|
| 112 |
-
<img src="https://raw.githubusercontent.com/dhruvshah08/Daease-assets/main/logo.png" alt="Daease Logo" class="logo">
|
| 113 |
-
<h2><span class="purple-text">Daease</span> Medical Report</h2>
|
| 114 |
-
</div>
|
| 115 |
-
|
| 116 |
-
<div class="content">
|
| 117 |
-
<p>Dear {patient_name},</p>
|
| 118 |
-
|
| 119 |
-
<p>Thank you for using <strong class="purple-text">Daease Medical Assistant</strong>. Your medical report from the consultation on {current_date} is attached to this email.</p>
|
| 120 |
-
|
| 121 |
-
<div class="summary">
|
| 122 |
-
<h3>Consultation Summary:</h3>
|
| 123 |
-
<p>{conversation_summary}</p>
|
| 124 |
-
</div>
|
| 125 |
-
|
| 126 |
-
<p><strong>Important:</strong> This report is for informational purposes only and should not replace professional medical advice from a licensed healthcare provider.</p>
|
| 127 |
-
|
| 128 |
-
<p>If you have any questions or require further assistance, please don't hesitate to contact us.</p>
|
| 129 |
-
|
| 130 |
-
<p>Best regards,<br>
|
| 131 |
-
<span class="purple-text">The Daease Medical Team</span></p>
|
| 132 |
-
</div>
|
| 133 |
-
|
| 134 |
-
<div class="footer">
|
| 135 |
-
<p>© {datetime.now().year} <span class="purple-text">Daease</span> | <a href="http://daease.com">Visit Our Website</a></p>
|
| 136 |
-
<p>Contact us: <a href="mailto:daease.main@gmail.com">daease.main@gmail.com</a></p>
|
| 137 |
-
<p>This email was sent to {to_email} because you requested a consultation report from Daease Medical Assistant.</p>
|
| 138 |
-
</div>
|
| 139 |
-
</div>
|
| 140 |
-
</body>
|
| 141 |
-
</html>
|
| 142 |
-
"""
|
| 143 |
-
|
| 144 |
-
# Plain text version as fallback
|
| 145 |
-
plain_text = f"""
|
| 146 |
-
Dear {patient_name},
|
| 147 |
-
|
| 148 |
-
Thank you for using Daease Medical Assistant. Your medical report from the consultation on {current_date} is attached to this email.
|
| 149 |
-
|
| 150 |
-
CONSULTATION SUMMARY:
|
| 151 |
-
{conversation_summary}
|
| 152 |
-
|
| 153 |
-
IMPORTANT: This report is for informational purposes only and should not replace professional medical advice from a licensed healthcare provider.
|
| 154 |
-
|
| 155 |
-
If you have any questions or require further assistance, please don't hesitate to contact us.
|
| 156 |
-
|
| 157 |
-
Best regards,
|
| 158 |
-
The Daease Medical Team
|
| 159 |
-
|
| 160 |
-
---
|
| 161 |
-
© {datetime.now().year} Daease | Website: http://daease.com
|
| 162 |
-
Contact us: daease.main@gmail.com
|
| 163 |
-
This email was sent to {to_email} because you requested a consultation report from Daease Medical Assistant.
|
| 164 |
-
"""
|
| 165 |
-
|
| 166 |
-
# Prepare email data with both HTML and plain text versions
|
| 167 |
email_data = {
|
| 168 |
"personalizations": [
|
| 169 |
{
|
| 170 |
"to": [{"email": to_email}],
|
| 171 |
-
"subject": f"
|
| 172 |
}
|
| 173 |
],
|
| 174 |
-
"from": {
|
| 175 |
-
"email": self.from_email,
|
| 176 |
-
"name": self.from_name
|
| 177 |
-
},
|
| 178 |
"content": [
|
| 179 |
{
|
| 180 |
"type": "text/plain",
|
| 181 |
-
"value":
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
}
|
| 187 |
],
|
| 188 |
"attachments": [
|
|
@@ -192,48 +71,7 @@ This email was sent to {to_email} because you requested a consultation report fr
|
|
| 192 |
"filename": pdf_filename,
|
| 193 |
"disposition": "attachment"
|
| 194 |
}
|
| 195 |
-
]
|
| 196 |
-
# Add spam prevention headers
|
| 197 |
-
"headers": {
|
| 198 |
-
"List-Unsubscribe": f"<mailto:unsubscribe@daease.com?subject=unsubscribe:{to_email}>",
|
| 199 |
-
"Precedence": "bulk",
|
| 200 |
-
"X-Entity-Ref-ID": f"daease-report-{safe_name}-{datetime.now().strftime('%Y%m%d%H%M%S')}",
|
| 201 |
-
"X-Report-Abuse": "Please report abuse here: abuse@daease.com",
|
| 202 |
-
"X-Auto-Response-Suppress": "OOF, DR, RN, NRN, AutoReply"
|
| 203 |
-
},
|
| 204 |
-
"categories": ["medical_report", "patient_communication"],
|
| 205 |
-
"custom_args": {
|
| 206 |
-
"report_type": "medical_consultation",
|
| 207 |
-
"user_requested": "true"
|
| 208 |
-
},
|
| 209 |
-
"asm": {
|
| 210 |
-
"group_id": 12345, # Replace with your actual unsubscribe group ID in SendGrid
|
| 211 |
-
"groups_to_display": [12345] # Replace with your actual group IDs
|
| 212 |
-
},
|
| 213 |
-
"mail_settings": {
|
| 214 |
-
"bypass_spam_management": {
|
| 215 |
-
"enable": False
|
| 216 |
-
},
|
| 217 |
-
"footer": {
|
| 218 |
-
"enable": False
|
| 219 |
-
},
|
| 220 |
-
"sandbox_mode": {
|
| 221 |
-
"enable": False
|
| 222 |
-
}
|
| 223 |
-
},
|
| 224 |
-
"tracking_settings": {
|
| 225 |
-
"click_tracking": {
|
| 226 |
-
"enable": True,
|
| 227 |
-
"enable_text": False
|
| 228 |
-
},
|
| 229 |
-
"open_tracking": {
|
| 230 |
-
"enable": True,
|
| 231 |
-
"substitution_tag": "%open-track%"
|
| 232 |
-
},
|
| 233 |
-
"subscription_tracking": {
|
| 234 |
-
"enable": True
|
| 235 |
-
}
|
| 236 |
-
}
|
| 237 |
}
|
| 238 |
|
| 239 |
# Send request to SendGrid API
|
|
|
|
| 2 |
import base64
|
| 3 |
import logging
|
| 4 |
import requests
|
|
|
|
| 5 |
|
| 6 |
# Configure logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 11 |
def __init__(self):
|
| 12 |
self.api_key = os.getenv('SENDGRID_API_KEY')
|
| 13 |
self.from_email = os.getenv('SENDGRID_FROM_EMAIL', 'noreply@daease.com')
|
|
|
|
| 14 |
|
| 15 |
if not self.api_key:
|
| 16 |
logger.warning("SendGrid API key not found in environment variables")
|
| 17 |
|
| 18 |
+
def send_report(self, to_email, pdf_data, patient_name):
|
| 19 |
"""
|
| 20 |
Send medical report via SendGrid's API
|
| 21 |
|
|
|
|
| 23 |
to_email (str): Recipient's email address
|
| 24 |
pdf_data (bytes): The PDF report as bytes
|
| 25 |
patient_name (str): Name of the patient
|
|
|
|
| 26 |
|
| 27 |
Returns:
|
| 28 |
tuple[bool, str]: (success status, message)
|
|
|
|
| 39 |
safe_name = patient_name.replace(' ', '_').replace(',', '').replace('.', '')
|
| 40 |
pdf_filename = f"medical_report_daease_{safe_name}.pdf"
|
| 41 |
|
| 42 |
+
# Prepare email data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
email_data = {
|
| 44 |
"personalizations": [
|
| 45 |
{
|
| 46 |
"to": [{"email": to_email}],
|
| 47 |
+
"subject": f"Medical Report - {patient_name}"
|
| 48 |
}
|
| 49 |
],
|
| 50 |
+
"from": {"email": self.from_email},
|
|
|
|
|
|
|
|
|
|
| 51 |
"content": [
|
| 52 |
{
|
| 53 |
"type": "text/plain",
|
| 54 |
+
"value": f"""Dear {patient_name},
|
| 55 |
+
Please find attached your medical report from your recent consultation.
|
| 56 |
+
|
| 57 |
+
Note: This report is for informational purposes only and should not be considered as a substitute for professional medical advice.
|
| 58 |
+
|
| 59 |
+
Best regards,
|
| 60 |
+
Daease Medical Team
|
| 61 |
+
|
| 62 |
+
Website: http://daease.com
|
| 63 |
+
Email: daease.main@gmail.com
|
| 64 |
+
"""
|
| 65 |
}
|
| 66 |
],
|
| 67 |
"attachments": [
|
|
|
|
| 71 |
"filename": pdf_filename,
|
| 72 |
"disposition": "attachment"
|
| 73 |
}
|
| 74 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
# Send request to SendGrid API
|