由于最近工作比较忙,后续博客可能更新不及时,哈哈

前言:

由于python对于微信推送不够友好,需要扫码登录,短信接口需要RMB。我就想到了qq邮箱发送到好友,然而微信有qq邮箱提醒功能,就实现了我需要定时推送消息的需求。

import smtplib,requests
from email.mime.text import MIMEText
from email.utils import formataddr
from apscheduler.schedulers.blocking import BlockingScheduler def send_mail(recipients,content):
#收件人 内容
ret=True
try:
msg=MIMEText(content,'plain','utf-8')
msg['From']=formataddr(["每日一乐",'396961930@qq.com']) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To']=formataddr(["",recipients]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject']="开心每一天" # 邮件的主题,也可以说是标题
server=smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是465
server.login('396961930@qq.com', '自己的秘钥') # smtp秘钥 server.sendmail('396961930@qq.com',[recipients,],msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit()# 关闭连接
except Exception:# 如果 try 中的语句没有执行,则会执行下面的 ret=False
ret=False
return ret def get_lovelive_info():
'''
从土味情话中获取每日一句。
:return: str,土味情话
'''
print('获取土味情话...')
resp = requests.get("https://api.lovelive.tools/api/SweetNothings")
if resp.status_code == 200:
return resp.text + "\n"
else:
print('每日一句获取失败')
return None
def send():
send_mail('396961930@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)")
send_mail('768158105@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)")
send_mail('595829154@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)")
send_mail('312161486@qq.com',get_lovelive_info()+"\t\t"+"(@Xcsg消息来自互联网)") if __name__ == "__main__":
#定时任务
scheduler = BlockingScheduler()
scheduler.add_job(send, 'cron', hour='7,22', minute='0')
scheduler.start()

dokcerfile

FROM python:3.7

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app/

RUN pip install -r /usr/src/app/requirements.txt

RUN rm -rf /usr/src/app

COPY . /usr/src/app

CMD [ "python3", "./message.py"]

requirements:

apscheduler
requests

打包好运行如下:

***** 里面有时区问题坑,注意

效果如下:

最新文章

  1. webapi post 请求多个参数
  2. C# WebBrowser控件使用教程与技巧收集
  3. nginx https性能优化
  4. 转载 Android快捷键 转载
  5. Installing Erlang
  6. img、input到底是行内还是块级元素?
  7. EF Code First:实体映射,数据迁移,重构(1)
  8. C++编程练习(14)-------“单例模式”的实现
  9. EFBaseDal
  10. Razor视图引擎
  11. bzoj1047-理想的正方形(二维单调队列)
  12. 也来“玩”Metro UI之磁贴
  13. 响应的系统设置的事件——重写onConfigurationChanged响应系统设置更改
  14. javascript核心概念之——数组
  15. scrapy相关:splash安装 A javascript rendering service 渲染
  16. 一次Spring Bean初始化顺序问题排查记录
  17. react-redux中的数据传递
  18. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
  19. python全栈开发day87~91-整个流程梳理、CRM功能、知识点梳理
  20. spring中afterPropertiesSet方法与init-method配置描述

热门文章

  1. Dynamics 365-表单元素取值/赋值
  2. 浅谈 STL
  3. Centos7安装Kubernetes k8s v1.16.0 国内环境
  4. 20200416_Centos 7.2 在安装系统之前把数据备份出来
  5. 树莓派RTL8723BU_LINUX驱动安装
  6. CoProcessFunction实战三部曲之三:定时器和侧输出
  7. 第7.21节 Python抽象类—register注册虚拟子类
  8. Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析
  9. PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象的iconSize属性
  10. PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的accepted、rejected和helpRequested信号