邮件发送自动化

python定时器
本文主要介绍了python中定时器的用法,及格式化输出的简单使用。因为工作中项目需要,会使用到定时器,所以记录一下。
# -*- coding = utf-8 -*-
# @Time: 2022/12/18 22:05
# @Author : 
# @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 : 
# @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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑着的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值