python USES the logging module to send mail code samples
- 2020-07-21 08:50:46
- OfStack
The logging module can not only record log, but also send emails, making it very easy to use
#coding=utf-8
'''''
Created on 2016-3-21
@author: Administrator
'''
import logging, logging.handlers
class EncodingFormatter(logging.Formatter):
def __init__(self, fmt, datefmt=None, encoding=None):
logging.Formatter.__init__(self, fmt, datefmt)
self.encoding = encoding
def format(self, record):
result = logging.Formatter.format(self, record)
if isinstance(result, unicode):
result = result.encode(self.encoding or 'utf-8')
return result
#zhangdongsheng@itouzi.com
errlog = logging.getLogger()
sh = logging.handlers.SMTPHandler("smtp.163.com", 'xigongda200608@163.com', '381084992@qq.com',
"logging from my app",
credentials=('xigongda200608', 'password'),
secure=())
errlog.addHandler(sh)
sh.setFormatter(EncodingFormatter('%(message)s', encoding='utf-8'))
errlog.error(u' Error appending file ')
conclusion
That's the end of this article's sample email code for python using the logging module. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!