赞
踩
有一天同步文件的时候,我想要知道每天同步文件情况,如有多少文件同步成功,有多少文件失败,每天都能告知我结果,经过一番了解之后,发现smtplib,而且smtplib模块是Python的标准库,通常情况下不需要额外安装。只要你已经安装了Python,就可以直接使用smtplib模块
- def send_email(smtp_server,sender_email,password,receiver_email,subject,body):
- """
- @param smtp_server: SMTP服务器
- @param sender_email: 发件人邮箱
- @param password: 邮箱授权码,注意不是密码
- @param receiver_email: 收件人邮箱
- @param subject: 主题
- @param body: 邮件内容
- @return:
- """
- # 设置SMTP服务器、发件人邮箱和收件人邮箱的详细信息
- # 设置邮件主题、正文和附件等信息
- msg = MIMEText(body, 'plain')
- msg['Subject'] = subject
- msg['From'] = sender_email
- msg['To'] = receiver_email
- # 连接SMTP服务器,并发送邮件
- try:
- with smtplib.SMTP_SSL(smtp_server) as server:
- server.login(sender_email, password)
- server.sendmail(sender_email, receiver_email, msg.as_string())
- server.quit()
- print("send success")
- except Exception as e:
- print(str(e))
可以直接通过上面的方法发送响应的邮件内容
注:邮箱的password是邮箱授权码,需要去官网上获取
- if __name__ == '__main__':
- subject = 'pdf网盘同步脚本'
- body = 'from x test email'
- smtp_server = 'smtp.163.com'
- sender_email = '*****@163.com'
- receiver_email = '1042798703@qq.com'
- password = '******'
- after_ = time.time()
- date_time = datetime.now().strftime("_%Y_%m")
- send_email(smtp_server, sender_email, password, receiver_email, subject, body)
- print("总用时:",time.time() - after_," s")
然后就可以直接发送了,我做的定时任务corn
0 2 */1 * *
丢在服务器上,每天执行一次告诉我当天同步结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。