2016-07-12 30 views
0

我用ssl和flask + socketio,我試圖發送郵件,但由於某種原因,發送郵件不工作。flask_mail,郵件卡在線程上,永遠不會發送

這裏是我的配置:

app.config['MAIL_SERVER']='smtp.gmail.com' 
app.config['MAIL_PORT'] = 465 
app.config['MAIL_USERNAME'] = '[email protected]' 
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='[email protected]', 
      recipients=['[email protected]']) 
    msg.body = "This is the email body" 
    mail.send(msg) 

    return "" 

而且這裏的錯誤日誌:

File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 307, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size]) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 721, in sendall v = self.send(data[count:]) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 687, in send v = self._sslobj.write(data) error: [Errno 32] Broken pipe

...

File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 156, in configure_host host = smtplib.SMTP_SSL(self.mail.server, self.mail.port) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 796, in init SMTP.init(self, host, port, local_hostname, timeout) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 256, in init (code, msg) = self.connect(host, port) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 316, in 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", line 801, in _get_socket new_socket = socket.create_connection((host, port), timeout) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 575, in create_connection raise err error: [Errno 65] No route to host

由於某些原因,我無法發送任何內容電子郵件。由於socketio + ssl包裝,它似乎卡住了。所以我不知道如何配置正確的方式

+0

在錯誤日誌中有'沒有路由到主機'。檢查從程序到服務器和端口的網絡連接。它可能被防火牆所禁止。 – VPfB

回答

0

我有同樣的問題,如果你在虛擬機工作檢查ssh端口的網絡連接,在我的情況下,我的工作與vagrant,I爲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 = '[email protected]', #user mail 
      MAIL_PASSWORD = 'xxx', 
      MAIL_USE_TLS = False, 
      MAIL_USE_SSL = True, 
      MAIL_DEFAULT_SENDER = '[email protected]', #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 = ["[email protected]"] 
    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