Python 3种邮件告警实现
smtplib
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'xxx@qq.com'
receiver = 'xxx@qq.com'
smtpserver = 'smtp.qq.com'
username = 'xxx@qq.com'
password = 'jykicbluddilbjgc'
msg = MIMEText( 'Hi', 'text', 'utf-8' )
subject = 'cool'
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = Header( subject, 'utf-8' )
smtp = smtplib.SMTP()
smtp.connect( smtpserver )
smtp.login( username, password )
smtp.sendmail( sender, receiver, msg.as_string() )
smtp.quit()
logging + SMTPHandler
import logging
LOGGER = logging.getLogger()
mail_lists = [
'xxx@qq.com',
]
hl = logging.handlers.SMTPHandler(
("smtp.qq.com", 25),
'xxx@qq.com',
mail_lists,
"Logging from wechat webapi watcher",
credentials=('xxx@qq.com', 'xxx'))
LOGGER.addHandler(hl)
print(LOGGER.handlers)
[ LOGGER.removeHandler(i) for i in LOGGER.handlers ]
print(LOGGER.handlers)
LOGGER.addHandler(hl)
LOGGER.setLevel(logging.WARNING)
print(LOGGER.handlers)
LOGGER.warning('test')