6

我正在使用 Flask 和 Flask-Mail 构建一个简单的联系页面。我按照本教程构建了应用程序 -添加联系页面- 现在,当我尝试发送消息时,我收到了错误gaierror: [Errno -2] Name or service not known。我一直在谷歌上搜索错误,但在网上找不到任何类似的例子。我什至无法弄清楚它找不到什么名称或服务。

回溯页面将让我展开一行并执行一些 Python 代码。它提供了一个dump()函数,可以显示所有变量,如果有帮助,可以在对象上调用以查看它们的信息。

路线.py:

from forms import ContactForm
from flask.ext.mail import Message, Mail


mail = Mail()
app = Flask(__name__)


app.secret_key = 'development key'

app.config['MAIL_SERVER'] = 'smtp.google.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'email'
app.config['MAIL_PASSWORD'] = 'password'

mail.init_app(app)
@app.route('/contact', methods=['GET', 'POST'])
def contact():
    form = ContactForm()

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

表格:

from flask.ext.wtf import Form, validators
from wtforms.fields import TextField, TextAreaField, SubmitField
import wtforms


class ContactForm(Form):
    name = TextField("Name", [wtforms.validators.Required('Please enter your name')])
    email = TextField("Email", [wtforms.validators.Required('Please enter your email'), wtforms.validators.Email()])
    subject = TextField("Subject", [wtforms.validators.Required('Please enter a subject')])
    message = TextAreaField("Message", [wtforms.validators.Required('Please enter a message')])
    submit = SubmitField("Send")
            flash('All fields are required.')
            return render_template('contact.html', form=form)
        else:
            msg = Message(form.subject.data, sender='imauld@gmail.com', recipients=['imauld@gmail.com'])
            msg.body = """From: %s <%s> %s""" % (form.name.data, form.email.data, form.message.data)
            mail.send(msg)
            return render_template('contact.html', success=True)
    elif request.method == 'GET':
        return render_template('contact.html', form=form)

回溯:

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app

response = self.make_response(self.handle_exception(e))

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception

reraise(exc_type, exc_value, tb)

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app

response = self.full_dispatch_request()

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request

rv = self.dispatch_request()

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "/home/ian/PycharmProjects/flaskapp/app/routes.py", line 39, in contact

mail.send(msg)

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask_mail.py", line 415, in send

with self.connect() as connection:

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask_mail.py", line 123, in __enter__

self.host = self.configure_host()

File "/home/ian/PycharmProjects/flaskapp/lib/python2.7/site-packages/flask_mail.py", line 135, in configure_host

host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)

File "/usr/lib/python2.7/smtplib.py", line 776, in __init__

SMTP.__init__(self, host, port, local_hostname, timeout)

File "/usr/lib/python2.7/smtplib.py", line 249, in __init__

(code, msg) = self.connect(host, port)

File "/usr/lib/python2.7/smtplib.py", line 309, in connect

self.sock = self._get_socket(host, port, self.timeout)

File "/usr/lib/python2.7/smtplib.py", line 781, in _get_socket

new_socket = socket.create_connection((host, port), timeout)

File "/usr/lib/python2.7/socket.py", line 553, in create_connection

for res in getaddrinfo(host, port, 0, SOCK_STREAM):

gaierror: [Errno -2] Name or service not known
4

1 回答 1

11

我有以下适合我的设置

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'xx@gmail.com'
app.config['MAIL_PASSWORD'] = 'xx;'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

请注意我拥有的 MAIL_USE_TLS 参数。也不确定这是否重要,但您的 MAIL_SERVER 设置为 smtp.google.com 而我的是 smtp.gmail.com。

编辑:经确认,主要问题是您的 smtp 主机设置不正确。它应该永远是smtp.gmail.com

于 2014-01-08T19:34:32.363 回答