0
votes

I'm using flask + socketio with ssl, and I'm trying to send mail, but for some reason, sending mail is not working.

Here is my configuration:

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)

...

And when I'm using it:

@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 ""

And here is the error log:

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

For some reason, I can't send any email. It seems stuck because of the socketio + ssl wrapping. I've got no idea on how to configure the right way.

1
There is No route to host in the error log. Check the network connection to the server and port from your program. It may be prohibited by a firewall.VPfB

1 Answers

1
votes

I had the same issue, if your are working in a virtual machine check the network connection for the ssh port, in my case i work with vagrant, I enabled connection for the ssh port, this code works to me:

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)

Also, you should verify if the built-insecurity features in Gmail service may block this login attempt. Log in your account and Visit https://www.google.com/settings/security/lesssecureapps.