Forums

[Errno 111] Connection refused - gmail smtp - paid account

Hi. I have a paid account, but I appear to be having trouble sending email via gmail's SMTP server. I'm using an app-specific password for gmail. The relevant code from my app:

from flask import Flask, render_template
from flaskext.mysql import MySQL
from flask import request
from flask_mail import Mail, Message

app = Flask(__name__)

mail = Mail(app)

app.config.update(
                    DEBUG=True,
                    MAIL_SERVER='smtp.gmail.com',
                    MAIL_PORT=465,
                    MAIL_USE_SSL=True,
                    MAIL_USERNAME = 'XXXX@gmail.com',
                    MAIL_PASSWORD = 'XXXXXXXXXXXXXXXX'
                    )

@app.route('/curated_entries/', methods = ['GET', 'POST'])
def curate_tweets():
   # other code that works omitted, pulling data from an html form
    msg = mail.send_message(
        'Hello',
        sender = 'XXXX@gmail.com',
        recipients = ['XXXX@XXXX.com'],
        body = "This is the email body")
    mail.send(msg)
    return render_template('confirmation.html', data=curated_entries)

Here is the error log. Any thoughts on what I'm doing wrong? Thanks.

2018-03-02 19:21:34,305: Error running WSGI application
2018-03-02 19:21:34,318: ConnectionRefusedError: [Errno 111] Connection refused
2018-03-02 19:21:34,318:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1994, in __call__
2018-03-02 19:21:34,318:     return self.wsgi_app(environ, start_response)
2018-03-02 19:21:34,318: 
2018-03-02 19:21:34,318:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
2018-03-02 19:21:34,319:     response = self.handle_exception(e)
2018-03-02 19:21:34,319: 
2018-03-02 19:21:34,319:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
2018-03-02 19:21:34,319:     reraise(exc_type, exc_value, tb)
2018-03-02 19:21:34,319: 
2018-03-02 19:21:34,320:   File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-03-02 19:21:34,320:     raise value
2018-03-02 19:21:34,320: 
2018-03-02 19:21:34,320:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
2018-03-02 19:21:34,320:     response = self.full_dispatch_request()
2018-03-02 19:21:34,321: 
2018-03-02 19:21:34,321:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
2018-03-02 19:21:34,321:     rv = self.handle_user_exception(e)
2018-03-02 19:21:34,321: 
2018-03-02 19:21:34,321:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
2018-03-02 19:21:34,322:     reraise(exc_type, exc_value, tb)
2018-03-02 19:21:34,322: 
2018-03-02 19:21:34,322:   File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-03-02 19:21:34,322:     raise value
2018-03-02 19:21:34,324: 
2018-03-02 19:21:34,324:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
2018-03-02 19:21:34,324:     rv = self.dispatch_request()
2018-03-02 19:21:34,325: 
2018-03-02 19:21:34,325:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
2018-03-02 19:21:34,325:     return self.view_functions[rule.endpoint](**req.view_args)
2018-03-02 19:21:34,325: 
2018-03-02 19:21:34,325:   File "/home/AccountName/mysite/curate_newsletter.py", line 49, in curate_tweets
2018-03-02 19:21:34,326:     body = "This is the email body")
2018-03-02 19:21:34,326: 
2018-03-02 19:21:34,326:   File "/home/AccountName/.local/lib/python3.6/site-packages/flask_mail.py", line 502, in send_message
2018-03-02 19:21:34,326:     self.send(Message(*args, **kwargs))
2018-03-02 19:21:34,326: 
2018-03-02 19:21:34,326:   File "/home/AccountName/.local/lib/python3.6/site-packages/flask_mail.py", line 491, in send
2018-03-02 19:21:34,327:     with self.connect() as connection:
2018-03-02 19:21:34,327: 
2018-03-02 19:21:34,327:   File "/home/AccountName/.local/lib/python3.6/site-packages/flask_mail.py", line 144, in __enter__
2018-03-02 19:21:34,327:     self.host = self.configure_host()
2018-03-02 19:21:34,327: 
2018-03-02 19:21:34,327:   File "/home/AccountName/.local/lib/python3.6/site-packages/flask_mail.py", line 158, in configure_host
2018-03-02 19:21:34,327:     host = smtplib.SMTP(self.mail.server, self.mail.port)
2018-03-02 19:21:34,328: 
2018-03-02 19:21:34,328:   File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
2018-03-02 19:21:34,332:     (code, msg) = self.connect(host, port)
2018-03-02 19:21:34,332: 
2018-03-02 19:21:34,332:   File "/usr/lib/python3.6/smtplib.py", line 335, in connect
2018-03-02 19:21:34,333:     self.sock = self._get_socket(host, port, self.timeout)
2018-03-02 19:21:34,333: 
2018-03-02 19:21:34,333:   File "/usr/lib/python3.6/smtplib.py", line 306, in _get_socket
2018-03-02 19:21:34,333:     self.source_address)
2018-03-02 19:21:34,333: 
2018-03-02 19:21:34,333:   File "/usr/lib/python3.6/socket.py", line 722, in create_connection
2018-03-02 19:21:34,334:     raise err
2018-03-02 19:21:34,334: 
2018-03-02 19:21:34,334:   File "/usr/lib/python3.6/socket.py", line 713, in create_connection
2018-03-02 19:21:34,334:     sock.connect(sa)

And here's the WSGI file, which I don't recall altering:

import sys

# add your project directory to the sys.path
project_home = u'/home/AccountName/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from curate_newsletter import app as application

One thing to check- can you run similar code from the console and get it working? If that doesn't work, can you run code locally and get that working?

Thanks, Conrad. The solution ended up being to move the line "mail = Mail(app)" to after "app.config" – it was initiating before the user credentials had been specified:

app.config.update(
                    DEBUG=True,
                    MAIL_SERVER='smtp.gmail.com',
                    MAIL_PORT=465,
                    MAIL_USE_SSL=True,
                    MAIL_USERNAME = 'XXXX@gmail.com',
                    MAIL_PASSWORD = 'XXXXXXXXXXXXXXXX'
                    )

mail = Mail(app)

Ah! That makes sense :D