首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >curent应用程序未配置Flask-Mail

curent应用程序未配置Flask-Mail
EN

Stack Overflow用户
提问于 2019-07-02 19:04:17
回答 1查看 878关注 0票数 2

我正在使用celery发送邮件,但是我收到了引发RuntimeError的错误(“当前的应用程序没有配置Flask- mail ")

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

代码语言:javascript
复制
`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时得到的错误

代码语言:javascript
复制
celery -A index.celery worker -l info -P gevent
代码语言:javascript
复制
[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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-02 19:17:17

代码语言:javascript
复制
 mail.init_app(app)

我还没有在配置时使用init_app方法添加邮件实例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56851051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档