python 定时发送邮件
2021/12/31 20:38:59
本文主要是介绍python 定时发送邮件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header def send_email(sender, receiver, text, subject): """ 发送纯文本邮件 :param sender: :param receiver: :param text: :param subject: :return: """ mail_host = 'smtp.qq.com' # qq邮箱服务地址 smt = smtplib.SMTP() # #实例化SMTP类,这样就可以调用SMTP对象的方法和属性 smt.connect(mail_host, '25') # 连接服务器,第二个参数为端口号 password = "mmmmmm" # 邮箱登录密码(需要进入qq邮箱->设置->账号->获取密码) try: smt.login(sender, password) # 登录邮箱,第一参数是发件人邮箱账号、第二个为邮箱登录密码 message = MIMEText(text, 'plain', 'utf-8') # 实例化一个MIMEText邮件对象,该对象需要写进三个参数,分别是邮件正文,文本格式和编码 message['Subject'] = Header(subject, 'utf-8') # 邮件标题 try: smt.sendmail(sender, receiver, message.as_string()) # 发送邮件操作 print("邮件发送成功") except: print("邮件发送失败") except: print("登录失败,请检测邮箱、密码是否正确") smt.quit() # 退出邮箱
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header def send_file(sender, receiver, file, subject): """ 发送带附件的邮件 :param sender: :param receiver: :param data: :param subject: :return: """ addr = 'smtp.qq.com' smt = smtplib.SMTP() smt.connect(addr, '25') password = "mmmmmm" smt.login(sender, password) message = MIMEMultipart() message['Subject'] = Header(subject, 'utf-8') message['From'] = Header(sender, 'utf-8') message['To'] = Header(receiver, 'utf-8') message.attach(MIMEText("最近一周天气情况", 'plain', 'utf-8')) att1 = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8') att1['Content-Type'] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment, filename="weather.txt' message.attach(att1) smt.sendmail(sender, receiver, message.as_string())
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header import schedule import time def job(): """ 定时发送邮件 :return: """ file = writer_data(get_data()) send_file(sender, receiver, file, subject) schedule.every(2).minutes.do(job) while True: schedule.run_pending() time.sleep(2)
这篇关于python 定时发送邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享
- 2024-12-19使用python 把docx转为pdf文件有哪些方法?-icode9专业技术文章分享
- 2024-12-19python怎么更换换pip的源镜像?-icode9专业技术文章分享
- 2024-12-19Python资料:新手入门的全面指南
- 2024-12-19Python股票自动化交易实战入门教程
- 2024-12-19Python股票自动化交易入门教程
- 2024-12-18Python量化入门教程:轻松掌握量化交易基础知识