0

我正在尝试遵循本教程。当我尝试提交应该触发电子邮件的联系表单时,我收到内部服务器错误。错误日志说:

RuntimeError: The curent application was not configured with Flask-Mail

说明说用于from flask.ext.mail导入,但我已经看到它可能是from flask_mail现在。我还尝试将邮件端口从 465 更改为 587。这些更改都没有解决问题。我最新的代码是:

from flask import Flask, render_template, request, flash
from forms import ContactForm
from flask_mail import Mail, Message

mail = Mail()

app = Flask(__name__)

app.secret_key = 'development key'

app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 587
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = 'contact_email@gmail.com'  ## CHANGE THIS
app.config["MAIL_PASSWORD"] = 'password'

mail.init_app(app)

app = Flask(__name__)
app.secret_key = 'Oh Wow This Is A Super Secret Development Key'


@app.route('/')
def home():
  return render_template('home.html')

@app.route('/about')
def about():
  return render_template('about.html')

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

  if request.method == 'POST':
    if form.validate() == False:
      flash('All fields are required.')
      return render_template('contact.html', form=form)
    else:
      msg = Message(form.subject.data, sender='contact_email@gmail.com', recipients=['recipient@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)

if __name__ == '__main__':
    app.run(debug=True)
4

1 回答 1

2

在配置初始应用程序后,您创建了第二个应用程序(可能是偶然的)。现在“第一个”app已配置并注册了扩展名,但“第二个”app用于注册路由和呼叫.run()

删除之后的行,即创建另一个应用程序mail.init_app(app)的第二行。app = Flask(__name__)

于 2015-01-05T12:01:13.380 回答