Create sendgrid_service.py
Browse files- src/sendgrid_service.py +110 -0
src/sendgrid_service.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import base64
|
3 |
+
import logging
|
4 |
+
import requests
|
5 |
+
|
6 |
+
# Configure logging
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
class SendGridService:
|
11 |
+
def __init__(self):
|
12 |
+
self.api_key = os.getenv('SENDGRID_API_KEY')
|
13 |
+
self.from_email = os.getenv('SENDGRID_FROM_EMAIL', '[email protected]')
|
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 |
+
|
22 |
+
Args:
|
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)
|
29 |
+
"""
|
30 |
+
try:
|
31 |
+
# Check if API key is configured
|
32 |
+
if not self.api_key:
|
33 |
+
return False, "Email service not configured. Please contact support."
|
34 |
+
|
35 |
+
# Encode PDF data as base64
|
36 |
+
pdf_base64 = base64.b64encode(pdf_data).decode('utf-8')
|
37 |
+
|
38 |
+
# Prepare email data
|
39 |
+
email_data = {
|
40 |
+
"personalizations": [
|
41 |
+
{
|
42 |
+
"to": [{"email": to_email}],
|
43 |
+
"subject": f"Medical Report - {patient_name}"
|
44 |
+
}
|
45 |
+
],
|
46 |
+
"from": {"email": self.from_email},
|
47 |
+
"content": [
|
48 |
+
{
|
49 |
+
"type": "text/plain",
|
50 |
+
"value": f"""Dear {patient_name},
|
51 |
+
|
52 |
+
Please find attached your medical report from your recent consultation.
|
53 |
+
|
54 |
+
Note: This report is for informational purposes only and should not be considered as a substitute for professional medical advice.
|
55 |
+
|
56 |
+
Best regards,
|
57 |
+
Daease Medical Team
|
58 |
+
"""
|
59 |
+
}
|
60 |
+
],
|
61 |
+
"attachments": [
|
62 |
+
{
|
63 |
+
"content": pdf_base64,
|
64 |
+
"type": "application/pdf",
|
65 |
+
"filename": f"medical_report_{patient_name.replace(' ', '_')}.pdf",
|
66 |
+
"disposition": "attachment"
|
67 |
+
}
|
68 |
+
]
|
69 |
+
}
|
70 |
+
|
71 |
+
# Send request to SendGrid API
|
72 |
+
response = requests.post(
|
73 |
+
"https://api.sendgrid.com/v3/mail/send",
|
74 |
+
headers={
|
75 |
+
"Authorization": f"Bearer {self.api_key}",
|
76 |
+
"Content-Type": "application/json"
|
77 |
+
},
|
78 |
+
json=email_data
|
79 |
+
)
|
80 |
+
|
81 |
+
# Process response
|
82 |
+
if response.status_code == 202: # SendGrid returns 202 when successful
|
83 |
+
logger.info(f"Successfully sent report to {to_email}")
|
84 |
+
return True, "Report sent successfully!"
|
85 |
+
else:
|
86 |
+
logger.error(f"SendGrid API error: Status {response.status_code}, Body: {response.text}")
|
87 |
+
return False, f"Failed to send email (Error {response.status_code})"
|
88 |
+
|
89 |
+
except requests.exceptions.RequestException as e:
|
90 |
+
logger.error(f"Request error: {str(e)}")
|
91 |
+
return False, f"Connection error: {str(e)}"
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
logger.error(f"Unexpected error: {str(e)}")
|
95 |
+
return False, "An unexpected error occurred. Please try again later."
|
96 |
+
|
97 |
+
@staticmethod
|
98 |
+
def validate_email(email):
|
99 |
+
"""
|
100 |
+
Basic email validation
|
101 |
+
|
102 |
+
Args:
|
103 |
+
email (str): Email address to validate
|
104 |
+
|
105 |
+
Returns:
|
106 |
+
bool: True if valid email format
|
107 |
+
"""
|
108 |
+
import re
|
109 |
+
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
|
110 |
+
return bool(re.match(pattern, email))
|