Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -248,56 +248,61 @@ def document_data_tool(question):
|
|
248 |
|
249 |
|
250 |
def send_email_with_attachment(recipient_email, subject, body, image_data):
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
301 |
# return 1
|
302 |
|
303 |
|
|
|
248 |
|
249 |
|
250 |
def send_email_with_attachment(recipient_email, subject, body, image_data):
|
251 |
+
try:
|
252 |
+
sender_email = os.getenv("EMAIL_SENDER")
|
253 |
+
sender_password = os.getenv("EMAIL_PASSWORD")
|
254 |
+
|
255 |
+
# Create a multipart message
|
256 |
+
msg = MIMEMultipart()
|
257 |
+
msg['From'] = sender_email
|
258 |
+
msg['To'] = recipient_email
|
259 |
+
msg['Subject'] = subject
|
260 |
+
|
261 |
+
# Attach the body with the msg instance
|
262 |
+
msg.attach(MIMEText(body, 'plain'))
|
263 |
+
"""
|
264 |
+
# Open the file to be sent
|
265 |
+
attachment = open(attachment_path, "rb")
|
266 |
+
print("Attached the image")
|
267 |
+
# Instance of MIMEBase and named as p
|
268 |
+
part = MIMEBase('application', 'octet-stream')
|
269 |
+
|
270 |
+
# To change the payload into encoded form
|
271 |
+
part.set_payload((attachment).read())
|
272 |
+
|
273 |
+
# Encode into base64
|
274 |
+
encoders.encode_base64(part)
|
275 |
+
|
276 |
+
part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")
|
277 |
+
|
278 |
+
# Attach the instance 'part' to instance 'msg'
|
279 |
+
msg.attach(part)"""
|
280 |
+
# Create a MIMEBase instance for the attachment
|
281 |
+
part = MIMEBase('application', 'octet-stream')
|
282 |
+
|
283 |
+
# Set the payload with the image data
|
284 |
+
part.set_payload(image_data)
|
285 |
+
|
286 |
+
# Encode the payload into base64
|
287 |
+
encoders.encode_base64(part)
|
288 |
+
|
289 |
+
# Add a header with the filename (you can set any filename you prefer)
|
290 |
+
part.add_header('Content-Disposition', 'attachment; filename="image.png"')
|
291 |
+
|
292 |
+
# Attach the instance 'part' to the instance 'msg'
|
293 |
+
msg.attach(part)
|
294 |
+
|
295 |
+
# Create SMTP session for sending the mail
|
296 |
+
server = smtplib.SMTP('smtp.gmail.com', 587)
|
297 |
+
server.starttls()
|
298 |
+
server.login(sender_email, sender_password)
|
299 |
+
text = msg.as_string()
|
300 |
+
server.sendmail(sender_email, recipient_email, text)
|
301 |
+
server.quit()
|
302 |
+
|
303 |
+
except error:
|
304 |
+
print error
|
305 |
+
|
306 |
# return 1
|
307 |
|
308 |
|