Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Mail - Connection Refused [Errno 111]

I am using Flask-Mail in small web application. Since the application is small I am using gmail to send emails. After following all the steps in the documentation, when I run the app to test the email feature. I keep on getting a error: [Errno 111] Connection refused.

This are my setting for email in my config.py:

MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = '[email protected]'
MAIL_PASSWORD = 'some_password'
DEFAULT_MAIL_SENDER = '[email protected]'

This is the view I am using to test Flask-Mail in my views.py:

@app.route('/', methods=['POST', 'GET'])
def index():

    form = ContactForm()

    if request.method == 'POST':
        if form.validate():

            msg = Message('New Msg - Test',
                          recipients=['[email protected]'])

            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 to see if there were any firewall issues like this:

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 really sure about what causing the connection to be refused. I would appreciate if someone can give me a push in the right direction on how to further test or point where I am making a mistake.

like image 548
lv10 Avatar asked Apr 08 '13 14:04

lv10


1 Answers

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

MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL = True
MAIL_USERNAME = '[email protected]'
MAIL_PASSWORD = 'some_password'
DEFAULT_MAIL_SENDER = '[email protected]'
like image 52
codegeek Avatar answered Oct 13 '22 13:10

codegeek