【日志记录】发送邮件的python脚本
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime
def send_email_report():
# 邮件发送者和接收者
from_email = "your_email@example.com"
to_email = "recipient_email@example.com"
# SMTP服务器设置
smtp_server = "smtp.example.com"
smtp_port = 587
smtp_user = "your_email@example.com"
smtp_password = "your_password"
# 创建邮件
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = f"每日报告 - {datetime.now().strftime('%Y-%m-%d')}"
# 报告内容(可以根据需求生成或从文件中读取)
report_content = "这是您的每日报告内容。"
# 添加邮件正文
msg.attach(MIMEText(report_content, 'plain'))
# 连接到SMTP服务器并发送邮件
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls() # 启用TLS加密
server.login(smtp_user, smtp_password)
server.sendmail(from_email, to_email, msg.as_string())
print("邮件发送成功")
except Exception as e:
print(f"邮件发送失败: {e}")
finally:
server.quit()
if __name__ == "__main__":
send_email_report()