当前应用程序未配置Flask-Mail

时间:2019-07-02 11:04:17

标签: flask celery

我正在使用celery发送邮件,但出现了引发RuntimeError(“当前应用程序未配置Flask-Mail”)的错误

我在另一个文件celery_setup.py中有celery设置 Redis工作正常,我不知道这里出了什么问题。

`from flask import Flask,render_template,make_response
from flask_mail import Mail,Message
import pdfkit
import os
from celery_setup import make_celery
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)

app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] =os.environ.get('userid')
app.config['MAIL_PASSWORD'] = os.environ.get('password')

celery =make_celery(app)
db =SQLAlchemy(app)
mail=Mail()

class User(db.Model):
    username = db.Column(db.String(30),primary_key=True)
    email=db.Column(db.String(30))

@app.route('/email')
def send_email():
    send.delay()
    return "check your email"

@celery.task
def send():
    user = User.query.first()
    msg = Message('hello there',sender = mymail@gmail.com',recipients=[user.email])
    mail.send(msg)
    return "success"


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

这是我使用

运行芹菜时遇到的错误
celery -A index.celery worker -l info -P gevent
[2019-07-02 16:09:15,549: INFO/MainProcess] Connected to redis://localhost:6379/0
[2019-07-02 16:09:15,589: INFO/MainProcess] mingle: searching for neighbors
[2019-07-02 16:09:16,667: INFO/MainProcess] mingle: all alone
[2019-07-02 16:09:16,696: INFO/MainProcess] pidbox: Connected to redis://localhost:6379/0.
[2019-07-02 16:09:16,708: INFO/MainProcess] celery@DESKTOP-4U5GTVP ready.
[2019-07-02 16:11:51,582: INFO/MainProcess] Received task: index.send[e17b63d3-7b31-4503-b88c-36e138c19294]
[2019-07-02 16:11:51,653: ERROR/MainProcess] Task index.send[e17b63d3-7b31-4503-b88c-36e138c19294] raised unexpected: RuntimeError('The curent application was not configured with Flask-Mail')
Traceback (most recent call last):
  File "c:\users\ayada\appdata\local\programs\python\python37-32\lib\site-packages\flask_mail.py", line 508, in connect
    return Connection(app.extensions['mail'])
KeyError: 'mail'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\ayada\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\trace.py", line 385, in trace_task
    R = retval = fun(*args, **kwargs)
  File "D:\python resources\celery\genrate-and-send-pdf-using-celery\celery_setup.py", line 13, in __call__
    return TaskBase.__call__(self, *args, **kwargs)
  File "c:\users\ayada\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\trace.py", line 648, in __protected_call__
    return self.run(*args, **kwargs)
  File "D:\python resources\celery\genrate-and-send-pdf-using-celery\index.py", line 49, in send
    mail.send(msg)
  File "c:\users\ayada\appdata\local\programs\python\python37-32\lib\site-packages\flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "c:\users\ayada\appdata\local\programs\python\python37-32\lib\site-packages\flask_mail.py", line 510, in connect
    raise RuntimeError("The curent application was not configured with Flask-Mail")
RuntimeError: The curent application was not configured with Flask-Mail

1 个答案:

答案 0 :(得分:0)

只需添加一行:

class VC: UIViewController {
    @IBOutlet var buttons: [UIButton]!

    override func viewDidLoad() {
        super.viewDidLoad()
        buttons.forEach {
            $0.setTitleColor(.orange, for: .selected)
            $0.setTitleColor(.white, for: .normal)
        }
        buttons.first?.isSelected = true
    }

    @IBAction func onTapChangeColor(_ sender: UIButton) {
        buttons.forEach {
            $0.isSelected = ($0 == sender)
        }
    }
}

您尚未在配置时使用init_app方法添加Mail实例