[英]How to configure Flask Mail properly
我正在嘗試按照本教程進行操作 。 當我嘗試提交聯系表單時,該表單會觸發電子郵件,但出現內部服務器錯誤。 錯誤日志顯示:
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)
配置初始應用程序后,您創建了第二個應用程序(可能是偶然的)。 現在,“第一個” app
已配置並已注冊擴展名,但“第二個” app
用於注冊路線並調用.run()
。
刪除mail.init_app(app)
之后的行,第二個app = Flask(__name__)
,它創建另一個應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.