邮件发送自动化
python定时器
本文主要介绍了python中定时器的用法,及格式化输出的简单使用。因为工作中项目需要,会使用到定时器,所以记录一下。
# -*- coding = utf-8 -*-
# @Time: 2022/12/18 22:05
# @Author : Wang
# @File : auto_timer3.py
# @Software : PyCharm
# -*- coding:utf-8 -*-
import datetime
import threading
def func():
print("haha")
# 如果需要循环调用,就要添加以下方法
timer = threading.Timer(86400, func)
timer.start()
# 获取现在时间
now_time = datetime.datetime.now()
print('现在的时间是:', now_time)
# 获取明天时间
next_time = now_time + datetime.timedelta(days=+1)
next_year = next_time.date().year
next_month = next_time.date().month
next_day = next_time.date().day
# print(next_time, next_year, next_month, next_day)
# 获取明天3点时间
next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + " 03:00:00", "%Y-%m-%d %H:%M:%S")
print('明天需要执行函数的时间是:', next_time)
# # 获取昨天时间
# last_time = now_time + datetime.timedelta(days=-1)
# 获取距离明天3点时间,单位为秒
timer_start_time = (next_time - now_time).total_seconds()
# 占位符数字从0开始
print("距离函数执行时间还有{0}秒。。。".format(timer_start_time))
print('距离函数执行时间还有%s。。。' % (next_time - now_time))
# 54186.75975
# 定时器,参数为(多少时间后执行,单位为秒,执行的方法)
timer = threading.Timer(timer_start_time, func)
timer.start()
本文参看python实现定时器
邮件自动化发送
以表格形式发送邮件
# -*- coding = utf-8 -*-
# @Time: 2022/12/18 11:19
# @Author : Wang
# @File : automail1.py
# @Software : PyCharm
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
from email.mime.image import MIMEImage
mail_host = "smtp.qq.com" #SMTP服务器地址
mail_sender = "10***@qq.com" #账号
mail_passwd = "dwhmnuzamhplbfgd" #授权码
msg = MIMEMultipart('related')
msg["Subject"] = "带有表格的邮件"
msg["From"] = mail_sender #发送人
msg["To"] = "10***@qq.com" #接收人
#html格式的邮件正文
content = '''
<html>
<body>
<p><strong>这是加粗字体,可以是一个标题</strong></p>
<p>下面是一个表格,表格格式可以随意设置</p>
<table width="500" bordercolor="black" border="1" cellspacing="0">
<tr>
<td><strong>列名1</strong></td>
<td><strong>列名2</strong></td>
<td><strong>列名3</strong></td>
</tr>
<tr>
<td>str1</td>
<td>10</td>
<td>2019-01-01</td>
</tr>
<tr>
<td>str2</td>
<td>20</td>
<td>2019-01-02</td>
</tr>
<tr>
<td>str3</td>
<td>30</td>
<td>2019-01-03</td>
</tr>
</table>
</body>
</html>
'''
msg.attach(MIMEText(content,'html','utf-8'))
## 发送邮件
s = smtplib.SMTP("smtp.qq.com") #实例化对象
s.connect(mail_host, 25) #连接qq邮箱服务器,端口号为25
s.ehlo() # 向邮箱发送SMTP 'ehlo' 命令
s.starttls()
s.login(mail_sender, mail_passwd) #登录邮箱
s.sendmail(mail_sender, ["10***@qq.com"], msg.as_string())
s.quit()
【华为OD机试真题】可以转到CSDN相关专栏订阅学习:https://blog.csdn.net/weixin_45541762/article/details/129903356
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】