Amirizaniani commited on
Commit
96de3d8
1 Parent(s): b4ad6ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
app.py CHANGED
@@ -13,6 +13,8 @@ from email.mime.text import MIMEText
13
  from email.mime.base import MIMEBase
14
  from email import encoders
15
  import os
 
 
16
 
17
  nltk.download('punkt')
18
  from nltk.tokenize import word_tokenize
@@ -165,30 +167,36 @@ def process_inputs(llm, file, relevance, diversity, email):
165
  csv_file = "questions.csv"
166
  df.to_csv(csv_file, index=False)
167
 
168
- # Email the CSV file
169
- sender_email = "[email protected]"
170
- sender_password = "opri fcxx crkh bvfj"
 
 
 
 
 
 
 
 
 
 
171
  receiver_email = email
172
  subject = "Your Submitted Questions"
173
  body = "Thank you for your submission. Please find attached the CSV file containing your questions."
174
 
175
- message = MIMEMultipart()
176
- message['From'] = sender_email
177
- message['To'] = receiver_email
178
- message['Subject'] = subject
179
- message.attach(MIMEText(body, 'plain'))
180
-
181
- with open(csv_file, "rb") as attachment:
182
- part = MIMEBase('application', 'octet-stream')
183
- part.set_payload(attachment.read())
184
- encoders.encode_base64(part)
185
- part.add_header('Content-Disposition', f"attachment; filename= questions.csv")
186
- message.attach(part)
187
-
188
- with smtplib.SMTP('smtp.gmail.com', 587) as server:
189
- server.starttls()
190
- server.login(sender_email, sender_password)
191
- server.sendmail(sender_email, receiver_email, message.as_string())
192
 
193
  return "Submitted"
194
 
 
13
  from email.mime.base import MIMEBase
14
  from email import encoders
15
  import os
16
+ from sendgrid import SendGridAPIClient
17
+ from sendgrid.helpers.mail import Mail, Attachment, FileContent, FileName, FileType, Disposition
18
 
19
  nltk.download('punkt')
20
  from nltk.tokenize import word_tokenize
 
167
  csv_file = "questions.csv"
168
  df.to_csv(csv_file, index=False)
169
 
170
+ # Read the CSV file and encode it for email attachment
171
+ with open(csv_file, "rb") as f:
172
+ encoded_file = base64.b64encode(f.read()).decode()
173
+
174
+ attachment = Attachment(
175
+ FileContent(encoded_file),
176
+ FileName('questions.csv'),
177
+ FileType('text/csv'),
178
+ Disposition('attachment')
179
+ )
180
+
181
+ # Email the CSV file using SendGrid
182
+ sender_email = "[email protected]" # Your verified sender email in SendGrid
183
  receiver_email = email
184
  subject = "Your Submitted Questions"
185
  body = "Thank you for your submission. Please find attached the CSV file containing your questions."
186
 
187
+ message = Mail(
188
+ from_email=sender_email,
189
+ to_emails=receiver_email,
190
+ subject=subject,
191
+ html_content=body
192
+ )
193
+ message.attachment = attachment
194
+
195
+ try:
196
+ sg = SendGridAPIClient('your_sendgrid_api_key') # Replace with your SendGrid API key
197
+ response = sg.send(message)
198
+ except Exception as e:
199
+ return f"Failed to send email: {e}"
 
 
 
 
200
 
201
  return "Submitted"
202