Socket.error: [Errno 111] Connection refused when submitting an email using flask-mail

Socket.error: [Errno 111] Connection refused when submitting an email using flask-mail

from flask_mail import Mail, Message
app = Flask(__name__)
mail = Mail(app)
app.config.update(
    #EMAIL SETTINGS
    MAIL_SERVER='smtp.gmail.com',
    MAIL_PORT=465,
    MAIL_USE_SSL=True,
    MAIL_USERNAME = '<myemail>@gmail.com',
    MAIL_PASSWORD = '<mypassword>'
)
@app.route('/',methods=['GET'])
def main_page():
    iform=iPhoneForm()
    return render_template("user.html", iform=iform)

@app.route('/email',methods=['POST'])
def email():
    msg = Message(
          'Hello',
       sender=request.body['email'],
       recipients=
           ['marshall.a.x@gmail.com'])
    msg.body = "This is the email body"
    mail.send(msg)
    return "Sent"

然后在页面上,我有:

<form action="{{ url_for('email') }}" method="POST">
                            <p id="contact-copy">Enter your email below and we will notify you when Snappie becomes available on the app store</p>
                            {{ iform.email.label }}
                            {{ iform.email }}
                            {{ iform.submit }}
                        </form>

如您所见,我正在使用 flask-wtforms 并询问 flask-mail。现在这是我在提交带有电子邮件地址的表单时遇到的错误:

2015-03-17 23:25:31,770 :/usr/lib/python2.7/threading.py:1160: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
2015-03-17 23:25:31,789 :  return _active[_get_ident()]
2015-03-17 23:25:31,790 :Traceback (most recent call last):
2015-03-17 23:25:31,790 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2015-03-17 23:25:31,790 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-03-17 23:25:31,790 :  File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-03-17 23:25:31,790 :    self.error(msg, *args, **kwargs)
2015-03-17 23:25:31,790 :  File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-03-17 23:25:31,791 :    self._log(ERROR, msg, args, **kwargs)
2015-03-17 23:25:31,791 :  File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-03-17 23:25:31,791 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-03-17 23:25:31,791 :  File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-03-17 23:25:31,791 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-03-17 23:25:31,792 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-03-17 23:25:31,792 :    self.threadName = threading.current_thread().name
2015-03-17 23:25:31,792 :  File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-03-17 23:25:31,792 :    return _active[_get_ident()]
2015-03-17 23:25:31,792 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2015-03-17 23:25:31,792 :    app_iterator = self.app(environ, start_response)
2015-03-17 23:25:31,792 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
2015-03-17 23:25:31,793 :    return self.wsgi_app(environ, start_response)
2015-03-17 23:25:31,793 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app
2015-03-17 23:25:31,793 :    response = self.make_response(self.handle_exception(e))
2015-03-17 23:25:31,793 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
2015-03-17 23:25:31,794 :    response = self.full_dispatch_request()
2015-03-17 23:25:31,794 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
2015-03-17 23:25:31,794 :    rv = self.handle_user_exception(e)
2015-03-17 23:25:31,794 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
2015-03-17 23:25:31,795 :    rv = self.dispatch_request()
2015-03-17 23:25:31,795 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
2015-03-17 23:25:31,795 :    return self.view_functions[rule.endpoint](**req.view_args)
2015-03-17 23:25:31,795 :  File "/home/snappiesticker/mysite/flask_app.py", line 85, in email
2015-03-17 23:25:31,797 :    mail.send(msg)
2015-03-17 23:25:31,797 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 400, in send
2015-03-17 23:25:31,806 :    with self.connect() as connection:
2015-03-17 23:25:31,806 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 102, in __enter__
2015-03-17 23:25:31,807 :    self.host = self.configure_host()
2015-03-17 23:25:31,807 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 116, in configure_host
2015-03-17 23:25:31,807 :    host = smtplib.SMTP(self.mail.server, self.mail.port)
2015-03-17 23:25:31,807 :  File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
2015-03-17 23:25:31,816 :    (code, msg) = self.connect(host, port)
2015-03-17 23:25:31,816 :  File "/usr/lib/python2.7/smtplib.py", line 311, in connect
2015-03-17 23:25:31,816 :    self.sock = self._get_socket(host, port, self.timeout)
2015-03-17 23:25:31,816 :  File "/usr/lib/python2.7/smtplib.py", line 286, in _get_socket
2015-03-17 23:25:31,817 :    return socket.create_connection((host, port), timeout)
2015-03-17 23:25:31,817 :  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
2015-03-17 23:25:31,820 :    raise err
2015-03-17 23:25:31,820 :socket.error: [Errno 111] Connection refused

这是什么意思,我该如何解决?

您在创建 Mail 对象后更新了 app 配置,因此它可能仍在使用默认服务器和端口(即 localhost:25)。尝试调换它们的顺序:

app = Flask(__name__)
app.config.update(
    #EMAIL SETTINGS
    MAIL_SERVER='smtp.gmail.com',
    MAIL_PORT=465,
    MAIL_USE_SSL=True,
    MAIL_USERNAME = '<myemail>@gmail.com',
    MAIL_PASSWORD = '<mypassword>'
)
mail = Mail(app)