YU-XI commited on
Commit
c430ca9
·
verified ·
1 Parent(s): e195599

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import smtplib
4
+ from email.mime.text import MIMEText
5
+ from email.mime.multipart import MIMEMultipart
6
+ from email.mime.base import MIMEBase
7
+ from email import encoders
8
+
9
+ # 讀取 Google 試算表資料
10
+ sheet_id = "1b3Ik50L8ciaE-40rcmUJTiEDP4dBFx0spuTCJGksBPU"
11
+ df1 = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")
12
+
13
+ def send_email(email):
14
+ # Convert the DataFrame to a CSV string
15
+ csv_data = df1.to_csv(index=False)
16
+
17
+ # Set up the email content
18
+ subject = "調查結果"
19
+ body = "請參閱附件中的調查結果。"
20
+
21
+ # Create a multipart message
22
+ msg = MIMEMultipart()
23
+ msg['From'] = '[email protected]'
24
+ msg['To'] = email
25
+ msg['Subject'] = subject
26
+
27
+ # Attach the body text
28
+ msg.attach(MIMEText(body, 'plain'))
29
+
30
+ # Attach the CSV file
31
+ part = MIMEBase('application', 'octet-stream')
32
+ part.set_payload(csv_data.encode('utf-8'))
33
+ encoders.encode_base64(part)
34
+ part.add_header('Content-Disposition', 'attachment; filename="survey_results.csv"')
35
+
36
+ msg.attach(part)
37
+
38
+ # Set up the server
39
+ server = smtplib.SMTP('smtp.gmail.com', 587)
40
+ server.starttls()
41
+ server.login('[email protected]', 'your_email_password')
42
+
43
+ # Send the email
44
+ text = msg.as_string()
45
+ server.sendmail('[email protected]', email, text)
46
+
47
+ server.quit()
48
+
49
+ return f"已將調查結果寄送到 {email}!"
50
+
51
+ # 使用 Gradio 構建前端界面
52
+ iface = gr.Interface(
53
+ fn=send_email,
54
+ inputs="text",
55
+ outputs="text",
56
+ title="調查結果寄送",
57
+ description="輸入您的電子郵件地址以接收調查結果。"
58
+ )
59
+
60
+ if __name__ == "__main__":
61
+ iface.launch()