File size: 1,736 Bytes
c430ca9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

# 讀取 Google 試算表資料
sheet_id = "1b3Ik50L8ciaE-40rcmUJTiEDP4dBFx0spuTCJGksBPU"
df1 = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")

def send_email(email):
    # Convert the DataFrame to a CSV string
    csv_data = df1.to_csv(index=False)

    # Set up the email content
    subject = "調查結果"
    body = "請參閱附件中的調查結果。"
    
    # Create a multipart message
    msg = MIMEMultipart()
    msg['From'] = '[email protected]'
    msg['To'] = email
    msg['Subject'] = subject
    
    # Attach the body text
    msg.attach(MIMEText(body, 'plain'))
    
    # Attach the CSV file
    part = MIMEBase('application', 'octet-stream')
    part.set_payload(csv_data.encode('utf-8'))
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="survey_results.csv"')
    
    msg.attach(part)
    
    # Set up the server
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('[email protected]', 'your_email_password')
    
    # Send the email
    text = msg.as_string()
    server.sendmail('[email protected]', email, text)
    
    server.quit()
    
    return f"已將調查結果寄送到 {email}!"

# 使用 Gradio 構建前端界面
iface = gr.Interface(
    fn=send_email,
    inputs="text",
    outputs="text",
    title="調查結果寄送",
    description="輸入您的電子郵件地址以接收調查結果。"
)

if __name__ == "__main__":
    iface.launch()