发送消息时禁用Flask-Mail中的日志记录

时间:2019-10-09 17:46:51

标签: python flask flask-mail

当通过Flask-Mail发送任何消息时,如下所示,带有mail.send(msg)的最后一行也将导致邮件标题和内容被记录。

Message("Hello", sender="from@example.com", recipients=["to@example.com"])
msg.body = 'anything'
mail.send(msg)

由于我的邮件可能包含敏感信息,因此我想完全禁用此日志记录。但是,在使用日志记录模块时,我找不到为Flask-Mail配置的记录器。

如何禁用Flask-Mail的登录?

1 个答案:

答案 0 :(得分:0)

想出了一个:

Flask-Mail使用Python的smtplib发送邮件。 smtplib不使用日志记录模块,但是它将信息输出以调试到stderr。

smtplib包括以下方法:

def set_debuglevel(self, debuglevel):
    """Set the debug output level.

    A non-false value results in debug messages for connection and for all
    messages sent to and received from the server.

    """
    self.debuglevel = debuglevel

如果我们使用Flask-Mail,则可以在初始化应用程序时设置此变量,如下所示:

app = Flask(__name__)
mail = Mail(app)
app.extensions['mail'].debug = 0

任何输出现在都被禁止。