简单粗暴,直接上代码。email通知在某些应用中还是必需的,当然也可以微信推送。
# -*- coding: utf-8 -*-
import time
# from playsound import playsound
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header
class EmailClient(object):
def __init__(self):
"""
初始化邮件列表
"""
self.to_list = ["receiver_email"]
def notification(self, body, subj):
sender = 'sender_email' # 邮件发送人
receiver = 'receiver_email' # 邮件收件人
subject = 'adslproxy error: ' + subj + ' ' + str(datetime.today())[:16] # 主题
smtpserver = 'smtp.163.com' # 网易的STMP地址 默认端口号为25
username = 'sender_email' # 发送邮件的人
password = EMAIPASS # 非网易登录密码.网易在开通SMTP服务后会有个密码设置
# 中文需参数‘utf-8',单字节字符不需要
msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8') # 头部信息:标题
msg['From'] = 'user<sender_email>' # 头部信息:名称<发件人的地址>
msg['To'] = ",".join(self.to_list) # 头部信息:收件人地址
m = True
while m == True:
try:
smtp = smtplib.SMTP_SSL('smtp.163.com', 465)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print('success')
m = False
except smtplib.SMTPException as e:
print('Error: ', e)
time.sleep(25)
emailclient = EmailClient()
emailclient.notification('test email', 'proxy')
在其他程序中调用时直接Import这个类即可:
from adslproxy.sendemail import EmailClient
比如:
try:
emailclient = EmailClient()
emailclient.notification('adslproxy started', 'ygg22457 adslproxy started')
print('email test success')
except Exception as e:
print(e)
本来还想用下微信推送的,后来想想太容易被封号,算了吧。