微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Gmail 帐户无法通过 Flask-Logging 工作

如何解决Gmail 帐户无法通过 Flask-Logging 工作

我一直在关注教程 - https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-error-handling,但无法使用我的 Gmail 帐户进行电子邮件日志记录。

在我的 init.py 中,我有

import logging
from logging.handlers import SMTPHandler

# ...

if not app.debug:
    if app.config['MAIL_SERVER']:
        auth = None
        if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
            auth = (app.config['MAIL_USERNAME'],app.config['MAIL_PASSWORD'])
        secure = None
        if app.config['MAIL_USE_TLS']:
            secure = ()
        mail_handler = SMTPHandler(
            mailhost=(app.config['MAIL_SERVER'],app.config['MAIL_PORT']),fromaddr='no-reply@' + app.config['MAIL_SERVER'],toaddrs=app.config['ADMINS'],subject='Microblog Failure',credentials=auth,secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

并设置环境变量

export MAIL_SERVER=smtp.gmail.com
export MAIL_PORT=587
export MAIL_USE_TLS=1
export MAIL_USERNAME=<your-gmail-username>
export MAIL_PASSWORD=<your-gmail-password>

我在我的 gmail 帐户上设置了“使用安全性较低的应用程序”并使用了普通密码,但我无法从本地计算机(在 localhost:5000 上运行)上的测试应用程序获得连接。我查看了 mail_handler 对象,我看到:

<SMTPHandler (ERROR)> 

因此在应用程序启动时没有建立连接。我花了一个小时来更改端口(587 和 465)等内容。设置新帐户。

如果有人有任何有用的想法,我将不胜感激。

谢谢

马丁

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。