Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python flask mail.send错误检查并记录_Python_Email_Flask - Fatal编程技术网

python flask mail.send错误检查并记录

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

我有一个Flask应用程序,邮件代码部分如下

   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)