Skip to content

Instantly share code, notes, and snippets.

@zh3389
Created April 28, 2024 08:39
Show Gist options
  • Save zh3389/fe8e775a96a58ba8b825399269ebb9df to your computer and use it in GitHub Desktop.
Save zh3389/fe8e775a96a58ba8b825399269ebb9df to your computer and use it in GitHub Desktop.
定时发送报告自动化根据数据库或文件内容,自动生成并定时发送日报/周报。
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def generate_report(source, to_addr, subject):
# 假设这里是从数据库或文件中获取数据并生成报告内容
report_content = pd.DataFrame({"Data": [1, 2, 3], "Info": ["A", "B", "C"]}).to_html()
msg = MIMEMultipart()
msg['From'] = 'your-email@example.com'
msg['To'] = to_addr
msg['Subject'] = subject
msg.attach(MIMEText(report_content, 'html'))
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login('your-email@example.com', 'your-password')
text = msg.as_string()
server.sendmail('your-email@example.com', to_addr, text)
server.quit()
# 使用示例:
generate_report('data.csv', 'receiver@example.com', '每日数据报告')
# 结合前面的定时任务脚本,可实现定时发送功能
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment