Redmind commited on
Commit
a6a2185
·
verified ·
1 Parent(s): 9fb4457

Update app.py

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