Yash goyal commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,9 @@ from reportlab.lib.units import inch
|
|
| 17 |
from datetime import datetime
|
| 18 |
import logging
|
| 19 |
from flask_mail import Mail, Message
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
app = Flask(__name__)
|
| 22 |
app.secret_key = "e3f6f40bb8b2471b9f07c4025d845be9"
|
|
@@ -152,24 +155,45 @@ def preprocess_image(image_bytes):
|
|
| 152 |
image_array = tf.keras.utils.img_to_array(image)
|
| 153 |
return np.expand_dims(image_array, axis=0) / 255.0
|
| 154 |
|
| 155 |
-
#
|
| 156 |
def send_email_async(app_context, report_data):
|
| 157 |
with app_context:
|
| 158 |
try:
|
|
|
|
| 159 |
pdf_path = f"/tmp/report_{report_data['scan_id']}.pdf"
|
| 160 |
generate_pdf(report_data, pdf_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
msg.body = f"Dear {report_data['name']},\n\nPlease find your diagnostic report attached."
|
| 164 |
-
|
| 165 |
-
with app.open_resource(pdf_path) as fp:
|
| 166 |
-
msg.attach(f"SnapSkin_Report_{report_data['scan_id']}.pdf", "application/pdf", fp.read())
|
| 167 |
-
|
| 168 |
-
mail.send(msg)
|
| 169 |
os.remove(pdf_path)
|
| 170 |
-
|
|
|
|
|
|
|
| 171 |
except Exception as e:
|
| 172 |
-
logger.error(f"Failed to send email
|
| 173 |
|
| 174 |
def generate_pdf(report, filepath):
|
| 175 |
c = canvas.Canvas(filepath, pagesize=A4)
|
|
|
|
| 17 |
from datetime import datetime
|
| 18 |
import logging
|
| 19 |
from flask_mail import Mail, Message
|
| 20 |
+
import base64
|
| 21 |
+
from sendgrid import SendGridAPIClient
|
| 22 |
+
from sendgrid.helpers.mail import (Mail, Attachment, FileContent, FileName, FileType, Disposition)
|
| 23 |
|
| 24 |
app = Flask(__name__)
|
| 25 |
app.secret_key = "e3f6f40bb8b2471b9f07c4025d845be9"
|
|
|
|
| 155 |
image_array = tf.keras.utils.img_to_array(image)
|
| 156 |
return np.expand_dims(image_array, axis=0) / 255.0
|
| 157 |
|
| 158 |
+
# REPLACE your old function with this new one
|
| 159 |
def send_email_async(app_context, report_data):
|
| 160 |
with app_context:
|
| 161 |
try:
|
| 162 |
+
# 1. Create the PDF report
|
| 163 |
pdf_path = f"/tmp/report_{report_data['scan_id']}.pdf"
|
| 164 |
generate_pdf(report_data, pdf_path)
|
| 165 |
+
|
| 166 |
+
# 2. Create the email message
|
| 167 |
+
message = Mail(
|
| 168 |
+
from_email='your-verified-email@gmail.com', # IMPORTANT: Use the email you verified on SendGrid
|
| 169 |
+
to_emails=report_data['email'],
|
| 170 |
+
subject='Your SnapSkin Diagnostic Report',
|
| 171 |
+
html_content=f"<strong>Dear {report_data['name']},</strong><p>Please find your diagnostic report from SnapSkin attached.</p>"
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# 3. Read the PDF and attach it to the email
|
| 175 |
+
with open(pdf_path, 'rb') as f:
|
| 176 |
+
data = f.read()
|
| 177 |
+
encoded_file = base64.b64encode(data).decode()
|
| 178 |
+
attachedFile = Attachment(
|
| 179 |
+
FileContent(encoded_file),
|
| 180 |
+
FileName(f"SnapSkin_Report_{report_data['scan_id']}.pdf"),
|
| 181 |
+
FileType('application/pdf'),
|
| 182 |
+
Disposition('attachment')
|
| 183 |
+
)
|
| 184 |
+
message.attachment = attachedFile
|
| 185 |
+
|
| 186 |
+
# 4. Use the API key from secrets to send the email
|
| 187 |
+
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
|
| 188 |
+
response = sg.send(message)
|
| 189 |
|
| 190 |
+
# 5. Clean up the created PDF file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
os.remove(pdf_path)
|
| 192 |
+
|
| 193 |
+
logger.info(f"Report sent via SendGrid, status code: {response.status_code}")
|
| 194 |
+
|
| 195 |
except Exception as e:
|
| 196 |
+
logger.error(f"Failed to send email via SendGrid: {e}")
|
| 197 |
|
| 198 |
def generate_pdf(report, filepath):
|
| 199 |
c = canvas.Canvas(filepath, pagesize=A4)
|