내 플라스크 프로젝트는 Flask-Cookiecutter을 기반으로하므로 전자 메일을 비동기 적으로 보내야합니다.Flask-Mail - Flask-Cookiecutter를 기반으로 전자 메일을 비동기 적으로 보냅니다.
전자 메일을 보내기위한 기능은 Miguel’s Tutorial으로 구성되었으며 동 기적으로 작동하는 것은 정상적으로 작동하지만 잘 모릅니다. 비동기 적으로 보낼 수 있도록 수정합니다. 내 로컬 호스트하고 명령을 시작 년대에 view.py
@blueprint.route('/mailer', methods=['GET', 'POST'])
def mailer():
user = current_user.full_name
send_email(('[email protected]'),
'New mail', 'test.html',
user=user)
return "Mail has been send."
응용 프로그램 실행에
내 app.py
def create_app(config_object=ProdConfig):
app = Flask(__name__)
app.config.from_object(config_object)
register_extensions(app)
register_blueprints(app)
register_errorhandlers(app)
return app
def register_extensions(app):
assets.init_app(app)
bcrypt.init_app(app)
cache.init_app(app)
db.init_app(app)
login_manager.init_app(app)
debug_toolbar.init_app(app)
migrate.init_app(app, db)
mail.init_app(app)
return None
내 view.py
from flask import current_app
@async
def send_async_email(current_app, msg):
with current_app.app_context():
print('##### spustam async')
mail.send(msg)
# Function for sending emails
def send_email(to, subject, template, **kwargs):
msg = Message(subject, recipients=[to])
msg.html = render_template('emails/' + template, **kwargs)
send_async_email(current_app, msg)
경로 :
내가 메일을 보내는 함수를 호출 할 때python manage.py server
, 콘솔의 출력은 다음과 같습니다 어떤 답변
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in a way. To solve
this set up an application context with app.app_context(). See the
documentation for more information.
감사합니다.
는 백그라운드 스레드에
감사합니다. 또한이 문제가 있습니다. – Pegasus