0

我正在使用带有ssl的flask + socketio,我正在尝试发送邮件,但由于某种原因,发送邮件不起作用。

这是我的配置:

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'xxx@gmail.com'
app.config['MAIL_PASSWORD'] = 'xxx;'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['DEBUG'] = True

mail=Mail(app)

...

当我使用它时:

@app.route('/testMail')
def testMail():
    msg = Message(
              'Hello',
           sender='xxx@gmail.com',
           recipients=['xxx@gmail.com'])
    msg.body = "This is the email body"
    mail.send(msg)

    return ""

这是错误日志:

文件“/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第 307 行,刷新 self._sock.sendall(view[write_offset :write_offset+buffer_size]) 文件“/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py”,第 721 行,在 sendall v = self .send(data[count:]) 文件“/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py”,第 687 行,在发送中 v = self._sslobj.write(data) 错误:[Errno 32] 管道损坏

...

文件“/usr/local/lib/python2.7/site-packages/flask_mail.py”,第 156 行,在 configure_host host = smtplib.SMTP_SSL(self.mail.server, self.mail.port) 文件“/usr/ local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”,第 796 行,在init SMTP 中。初始化(自我,主机,端口,local_hostname,超时)文件“/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”,第256行, 在初始化 (代码,味精)= self.connect(主机,端口)文件“/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”,第 316 行,在 connect self.sock = self._get_socket(host, port, self.timeout) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2. 7/smtplib.py”,第 801 行,在 _get_socket new_socket = socket.create_connection((host, port), timeout) 文件“/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7 /lib/python2.7/socket.py",第 575 行,在 create_connection 中 引发 err 错误:[Errno 65] 没有到主机的路由

由于某种原因,我无法发送任何电子邮件。由于 socketio + ssl 包装,它似乎卡住了。我不知道如何配置正确的方式。

4

1 回答 1

1

我遇到了同样的问题,如果您在虚拟机中工作,请检查ssh端口的网络连接,在我使用的情况下vagrant,我启用了ssh端口连接,此代码对我有用:

from flask import Flask
from flask_mail import Mail, Message

# object Flask and object Mail

app = Flask(__name__)
#configuration flask to Mail
app.config.update(
             MAIL_SERVER = "smtp.gmail.com",
             MAIL_PORT = 465,
             MAIL_USERNAME = 'xxx@gmail.com',  #user mail
             MAIL_PASSWORD = 'xxx',
             MAIL_USE_TLS = False,
             MAIL_USE_SSL = True,
             MAIL_DEFAULT_SENDER  =  'xxx@gmail.com', #user that will go email)
mail = Mail(app)

@app.route("/index")
def index():
    return "ok"


@app.route("/Test Mail")
def test_mail():
    """route to test email by flask mail."""
    msj = "This is a test to send mail with flask."
    recipients = ["xxxemail@gmail.com"]
    msg_object = Message("hello", recipients)
    msg_object.body = "Hello, this email is a test"
    mail.send(msg_object)
    return "Sent"



if __name__ == '__main__':
    app.run(host="0.0.0.0", port=7000, debug=True)

此外,您应该验证中的内置安全功能是否Gmail service会阻止此登录尝试。登录您的帐户并访问https://www.google.com/settings/security/lesssecureapps

于 2016-07-12T16:28:56.353 回答