Spaces:
Sleeping
Sleeping
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() |