Flask 邮件和 GMail 设置 - 非常困惑

问题描述 投票:0回答:2

我一直在学习 Flask(在许多非常慷慨的 YouTuber 的帮助下)
我认为添加通过 GMail 发送电子邮件通知的“订阅”功能会相当简单。
经过几天的谷歌搜索后,我从端口 465 或 587、SSL 和/或 TLS 启用/禁用的每种组合中得到以下结果。 Gmail 中允许使用不太安全的应用程序选项。

from flask import Flask  
from flask_mail import Mail, Message

app = Flask(__name__)

app.config['MAIL_DEBUG'] = True  
app.config['TESTING'] = False  
app.config['MAIL_SERVER'] = 'smtp.gmail.com'  
app.config['MAIL_PORT'] = see results below  
app.config['MAIL_USE_TLS'] = see results below  
app.config['MAIL_USE_SSL'] = see results below  
app.config['MAIL_USERNAME'] = None  
app.config['MAIL_PASSWORD'] = 'secret_password'  
app.config['MAIL_DEFAULT_SENDER'] = '[email protected]'  
app.config['MAIL_MAX_EMAILS'] = None  
app.config['MAIL_SUPPRESS_SEND'] = False  
app.config['MAIL_ASCII_ATTACHMENTS'] = False  

mail = Mail(app)

@app.route('/')  
def home():  
    msg = Message('Test Message from Flask Mail', recipients=['[email protected]'])  
    mail.send(msg)  
    return 'OK'

if __name__ == '__main__':  
    app.run(debug=True)

结果 -

Port    SSL TLS Result  
465 On  On  smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server  
465 Off On  smtplib.SMTPServerDisconnected: Connection unexpectedly closed  
465 On  Off smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required  
465 Off Off smtplib.SMTPServerDisconnected: Connection unexpectedly closed  
            
587 On  On  ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number  
587 Off On  smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required  
587 On  Off ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number  
587 Off Off smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first  

SSL 版本是 -

导入SSL
ssl.OPENSSL_VERSION
'OpenSSL 1.1.1f 2020 年 3 月 31 日'

这个 SMTPLIB 工作得很好 -

import smtplib

smtp_server = 'smtp.gmail.com'  
port = 587  
sender = '[email protected]'  
password = 'secret'  
receiver = '[email protected]'  
msg = "Test Message from SMTPLIB"  

server = smtplib.SMTP(smtp_server, port)  
server.starttls()  
server.login(sender,password)  
server.sendmail(sender, receiver, msg)  
ssl gmail smtplib flask-mail
2个回答
0
投票

已经过去几年了,但为我工作(查看旧代码)是

app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
app.config['MAIL_USERNAME'] = <your email address> # should not be None as you currently have in your code

注意:我仅使用此代码在本地计算机上进行测试。我切换到 Google App Engine 邮件,后来又切换到 SendGrid


0
投票

旧线程 - 但为了任何未来的寻求者的利益......

我认为 Flask-Mail 的

MAIL_USE_TLS
命名错误,真正的意思是使用 STARTTLS(即 Opportunistic TLS)。该协议预计在端口 587(提交)和/或 25(SMTP)上使用。最初的协商是明文的;此后,SSL/TLS 是“可选”协商的。如果第二次协商失败,客户端仍然可以继续并以明文形式发送邮件。 我认为(并希望)

MAIL_USE_SSL

也有点误导性

(但其本身并没有错误)
的命名,实际上意味着使用隐式SSL隐式TLS。实际上,现在应该总是使用 TLS。无论哪种方式,该协议都需要在端口 465 (SMTPS) 上。 SSL/TLS 立即且必要地建立; 否则连接失败

为了安全性和简单性,通常首选隐式 TLS。机会性 TLS 本身并不是问题 - 只要客户端配置为在发送敏感消息之前

需要成功进行 TLS 协商即可。在某些情况下,可能仍然需要机会性 TLS。 我已经使用多年了MAIL_PORT=465, MAIL_USE_SSL=True, MAIL_USE_TLS=False

;我的 MSA 仅接受隐式 TLS,并且仅接受 >= TLSv1.2 的协议,因此我确信 MAIL_USE_SSL 实际上支持隐式 TLS。

实际上,我刚刚检查了 Flask-Mail 代码(0.91),并确认了我对 MAIL_USE_TLS 的理解:它直接且唯一地导致了 STARTTLS 调用;对于 MAIL_USE_SSL,Flask-Mail 调用 smtplib 中的不同入口点来启用 SSL(相对于纯文本)客户端;根据经验,这显然支持现代 TLS。

这一切似乎仍然没有很好的记录:/

一般来说,要么:

MAIL_PORT=587; MAIL_USE_TLS=True;

或者,最好是:

MAIL_PORT=465; MAIL_USE_SSL=True;


    

© www.soinside.com 2019 - 2024. All rights reserved.