def flush(self):
if len(self.buffer) > 0:
msg = ""
for record in self.buffer:
s = self.format(record)
msg = msg + s + "\r\n"
# append a summary of severities
summary = {}
for record in self.buffer:
summary[record.levelname] = summary.get(record.levelname, 0) + 1
msg = msg + 'SUMMARY: ' + ', '.join(['%d %s(s)' % (v, k) for (k, v) in summary.items()]) + "\r\n"
# build the email
m = email.message.Message()
m['From'] = self.fromaddr
m['To'] = ','.join(self.toaddrs)
m['Bcc'] = common_constants.ALWAYS_BCC
m['Subject'] = self.subject
m['Message-Id'] = email.utils.make_msgid()
m['Date'] = email.utils.formatdate()
m['X-Calm'] = '1'
# use utf-8 only if the message can't be ascii encoded
charset = 'ascii'
try:
msg.encode('ascii')
except UnicodeError:
charset = 'utf-8'
m.set_payload(msg, charset=charset)
# if toaddrs consists of the single address 'debug', just dump the mail we would have sent
if self.toaddrs == ['debug']:
print('-' * 40)
print(msg)
print('-' * 40)
elif len(self.toaddrs) > 0:
try:
import smtplib
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port)
smtp.send_message(m)
smtp.quit()
except ImportError:
self.handleError(self.buffer[0]) # first record
self.buffer = []
评论列表
文章目录