0

I am trying to follow this tutorial. When I try to submit the contact form, which should trigger the email, I get an internal server error. The error log says:

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

The instructions say to use from flask.ext.mail to import but I've seen that it might be from flask_mail now. I've also tried changing the mail port from 465 to 587. Neither of these changes have been fixed the problem. My most up to date code is:

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)
davidism
  • 121,510
  • 29
  • 395
  • 339
ExperimentsWithCode
  • 1,174
  • 4
  • 14
  • 30

1 Answers1

2

You created a second app (presumably by accident) after configuring the initial app. Right now the "first" app is configured and has the extension registered, but the "second" app is used to register the routes and call .run().

Remove the line after mail.init_app(app), the second app = Flask(__name__) that creates another app.

davidism
  • 121,510
  • 29
  • 395
  • 339