python flask mail.send错误检查并记录
我有一个Flask应用程序,邮件代码部分如下python flask mail.send错误检查并记录,python,email,flask,Python,Email,Flask,我有一个Flask应用程序,邮件代码部分如下 if app.config['MAIL']: mail.send(message) else: print message.html 有时由于邮件服务器问题,mail.send功能会失败。如何检查错误状态 和日志一样 你觉得怎么样 if app.config['MAIL']: retcode=mail.send(message) else: print m
if app.config['MAIL']:
mail.send(message)
else:
print message.html
有时由于邮件服务器问题,mail.send功能会失败。如何检查错误状态
和日志一样
你觉得怎么样
if app.config['MAIL']:
retcode=mail.send(message)
else:
print message.html
# now log it
if (retcode != 0):
#log it or take anyother action.
尝试捕获异常:
if app.config['MAIL']:
try:
mail.send(message)
except SMTPException, e:
current_app.logger.error(e.message)
else:
print message.html
您可以根据SMTPException找到更多异常:
如果您确实需要返回代码,可以执行以下操作:
retcode = 0
if app.config['MAIL']:
try:
mail.send(message)
except SMTPAuthenticationError, e:
retcode = 2
except SMTPServerDisconnected, e:
retcode = 3
except SMTPException, e:
retcode = 1
else:
print message.html
if retcode:
current_app.logger.error(retcode)
尝试捕获异常:
if app.config['MAIL']:
try:
mail.send(message)
except SMTPException, e:
current_app.logger.error(e.message)
else:
print message.html
您可以根据SMTPException找到更多异常:
如果您确实需要返回代码,可以执行以下操作:
retcode = 0
if app.config['MAIL']:
try:
mail.send(message)
except SMTPAuthenticationError, e:
retcode = 2
except SMTPServerDisconnected, e:
retcode = 3
except SMTPException, e:
retcode = 1
else:
print message.html
if retcode:
current_app.logger.error(retcode)