在工作中,或者学习中或者生活上等,需要定时提醒自己或其他人,处理一些待办任务事项,需要发邮件通知下,同时可能会要求发送文件之类的事情。
由于现在大家微信使用频率最高,所以能发送微信通知消息,效果更好。
,同时开通下微信通知功能。
第一步:邮件通知工具
结合之前做过的发送邮件通知,再优化下,增加附件邮件发送功能;
10,Django实战项目-学习任务系统-发送邮件通知
https://mp.weixin.qq.com/s?__biz=Mzg2NDk2MTY3OA==&mid=2247484189&idx=1&sn=e974feca7b3169847b4c9a0e99015f57&chksm=ce6016fcf9179fea5eea0f477093750d261e9602c1bebbaee556bd02543b7fdabccdfd5eadec&token=1824279775&lang=zh_CN#rd
11,Django实战项目-学习任务系统-配置定时调度任务
https://mp.weixin.qq.com/s?__biz=Mzg2NDk2MTY3OA==&mid=2247484200&idx=1&sn=3584ec7126de0044a8424787d566ac06&chksm=ce6016c9f9179fdfe1480df5672ee0da61112a299e37dbc96e27c43f69b9b1ec5fe07fa3e76b&payreadticket=HGJwAEVrtVd3JpyUqg3aj8pQdCYzHTxJ0cZHPtbfghwMCKAggXw7YP7v-wqBId7gxT_s0ag#rd
mailutil.py :
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
def send_mail_util(sender, password, receiver, email_host, subject, content, file_path=None):
'''
@方法名称: 通用发送邮件工具
@中文注释: 通用发送邮件工具
@入参:
@param sender str 发送邮箱
@param password str 发送邮箱密码
@param receiver str 接收邮箱
@param email_host str 邮件服务器
@param subject str 邮件主题
@param content str 邮件内容
@param file_path str 邮件附件地址
@出参:
@返回状态:
@return 0 失败
@return 1 成功
@返回错误码
@返回错误信息
@param
@作 者: PandaCode辉
@weixin公众号: PandaCode辉
@创建时间: 2023-11-02
@使用范例:
'''
try:
if (not type(sender) is str):
return [0, "FBC001", "发送邮箱参数类型错误,不为字符串", [None]]
if (not type(password) is str):
return [0, "FBC001", "发送邮箱密码参数类型错误,不为字符串", [None]]
if (not type(receiver) is str):
return [0, "FBC001", "接收邮箱参数类型错误,不为字符串", [None]]
if (not type(email_host) is str):
return [0, "FBC001", "邮件服务器参数类型错误,不为字符串", [None]]
if (not type(subject) is str):
return [0, "FBC001", "邮件主题参数类型错误,不为字符串", [None]]
if (not type(content) is str):
return [0, "FBC001", "邮件内容参数类型错误,不为字符串", [None]]
# 你的收件邮箱地址列表
receivers = [receiver]
send_user = "<" + sender + ">"
print(send_user)
# message = MIMEText(content, 'plain', 'utf-8')
message = MIMEMultipart()
message['Subject'] = subject
message['From'] = send_user
message['To'] = ";".join(receivers)
# 设置邮件内容和格式
message.attach(MIMEText(content, 'plain', 'utf-8'))
# 附件文件不为空时
if file_path:
# 添加附件文件内容
with open(file_path, "rb") as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {file_path}")
message.attach(part)
server = smtplib.SMTP_SSL(email_host, 465)
server.login(sender, password)
server.sendmail(sender, receivers, message.as_string())
server.quit()
print('邮件发送成功')
return [1, '000000', '邮件发送成功', [receivers[0]]]
except Exception as e:
print("邮件发送失败异常," + str(e))
return [0, 'FBE999', "邮件发送失败异常," + str(e), [None]]
def send_qq_mail(subject, content, receiver, file_path=None):
'''
@方法名称: 发送QQ邮件方法
@中文注释: 发送QQ邮件方法
@入参:
@param receiver str 接收邮箱
@param subject str 邮件主题
@param content str 邮件内容
@param file_path str 邮件附件地址
@出参:
@返回状态:
@return 0 失败
@return 1 成功
@返回错误码
@返回错误信息
@param
@作 者: PandaCode辉
@weixin公众号: PandaCode辉
@创建时间: 2023-11-02
@使用范例:
'''
try:
if (not type(receiver) is str):
return [0, "FBC001", "接收邮箱参数类型错误,不为字符串", [None]]
if (not type(subject) is str):
return [0, "FBC001", "邮件主题参数类型错误,不为字符串", [None]]
if (not type(content) is str):
return [0, "FBC001", "邮件内容参数类型错误,不为字符串", [None]]
sender = '*********@@qq.com'
# 通过QQ邮箱设置获取的授权码,不是邮箱密码
password = '************'
# 设置服务器:这个是qq邮箱服务器
email_host = "smtp.qq.com"
# 通用发送邮件工具
rst = send_mail_util(sender, password, receiver, email_host, subject, content, file_path)
return rst
except Exception as e:
print("邮件发送失败异常," + str(e))
return [0, 'FBE999', "邮件发送失败异常," + str(e), [None]]
# 主方法
if __name__ == '__main__':
receiver = '*********@qq.com'
subject = '邮件主题,不带附件文件邮件'
content = '邮件内容,不带附件文件邮件'
# 发送qq邮件测试,不带附件文件邮件
send_qq_mail(subject, content, receiver)
subject = '邮件主题,带附件文件邮件'
content = '邮件内容,带附件文件邮件'
file_path = './file_path/test.xlsx'
# 发送qq邮件测试,带附件文件邮件
send_qq_mail(subject, content, receiver, file_path)
第二步:开通微信通知功能
怎么实现的微信通知呢?秘密就是把微信的邮件功能打开,如下:
微信设置
微信:我 → 设置?→?通用?→?辅助功能?→?QQ邮箱提醒?→?开启功能
这样就能让微信收到消息提醒了。
多说一句,请勿尝试用itchat、wxpy等第三方库操作微信,微信官方已停用这类工具,且存在封号风险!不信你就逝世!
第三步:运行效果
?
=======================end=======================