Flask-Mail - Connection Failure [Errno 111]

I use Flask-Mail in a small web application. Since the application is small, I use gmail to send emails. After completing all the steps in the documentation, when I run the application to check the email function. I keep getting error: [Errno 111] Connection refused .

This is my email setup in my config.py :

 MAIL_SERVER = 'smtp.gmail.com' MAIL_PORT = 587 MAIL_USE_TLS = True MAIL_USERNAME = ' some_user@gmail.com ' MAIL_PASSWORD = 'some_password' DEFAULT_MAIL_SENDER = ' some_user@gmail.com ' 

This is the view I use to check Flask-Mail in views.py :

 @app.route('/', methods=['POST', 'GET']) def index(): form = ContactForm() if request.method == 'POST': if form.validate(): msg = Message('New Msg - Test', recipients=[' some_user@gmail.com ']) msg.body = 'name=%s, email=%s \n Message:%s' % (form.name.data, form.email.data, form.message.data) mail.send(msg) flash('Message sent, I will contact you soon! Thanks') return redirect(url_for('index')) return render_template('index.html', form=form) 

I tested if there are any problems with the firewall:

 In [2]: import socket In [3]: sock = socket.socket() In [4]: sock.connect(("smtp.gmail.com", 587)) In [5]: sock.send("EHLO\n") Out[5]: 5 In [6]: sock.recv(1024) Out[6]: '220 mx.google.com ESMTP b9sm23940776vee.3 - gsmtp\r\n250-mx.google.com at your service, [108.21.9.10]\r\n250-SIZE 35882577\r\n250-8BITMIME\r\n250-STARTTLS\r\n250 ENHANCEDSTATUSCODES\r\n' 

I'm not quite sure what causes the connection to fail. I would appreciate it if someone could give me a push in the right direction on how to continue testing or indicate where I am making a mistake.

+6
source share
1 answer

I am using gmail SMTP for Flask-Mail, but with the following settings. I am using SSL with port 465. Maybe this will work for you?

 MAIL_SERVER = 'smtp.gmail.com' MAIL_PORT = 465 MAIL_USE_TLS = False MAIL_USE_SSL = True MAIL_USERNAME = ' some_user@gmail.com ' MAIL_PASSWORD = 'some_password' DEFAULT_MAIL_SENDER = ' some_user@gmail.com ' 
+1
source

All Articles