Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 626 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Flask-Mail asynchronously results in "RuntimeError: working outside of application context"

#1
I am trying to send some mail asynchronously (based on the code in [The Flask Mega-Tutorial, Part XI: Email Support][1]). However, I get the following error about working outside an application context. How do I fix this problem?

Traceback (most recent call last):
File "C:\Users\Primoz\Desktop\RecycleFlaskServer\recycleserver\helpers.py", line 17, in send_async_email
mail.send(msg)
File "C:\Python33\lib\site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py", line 434, in send
message.send(connection)
File "C:\Python33\lib\site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py", line 369, in send
connection.send(self)
File "C:\Python33\lib\site-packages\flask_mail-0.9.0-py3.3.egg\flask_mail.py", line 173, in send
email_dispatched.send(message, app=current_app._get_current_object())
File "C:\Python33\lib\site-packages\werkzeug-0.9.4-py3.3.egg\werkzeug\local.py", line 297, in _get_current_object
return self.__local()
File "C:\Python33\lib\site-packages\flask-0.10.1-py3.3.egg\flask\globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

<!-- break -->

from flask import Flask
from flask.ext.mail import Mail
app = Flask(__name__)
app.config.from_object('recycleserver.settings')
mail = Mail(app)

def async(f):
def wrapper(*args, **kwargs):
thr = Thread(target = f, args = args, kwargs = kwargs)
thr.start()
return wrapper

@async
def send_async_email(msg):
mail.send(msg)

def send_simple_mail(subject, sender, to_who, text_body="", html_body=""):
msg = Message(subject=subject,sender=sender, recipients=to_who)
msg.body = text_body
msg.html = html_body
send_async_email(msg)

[1]:

[To see links please register here]

Reply

#2
The code should run in an app context. Add `with app.app_context()`:

@async
def send_async_email(msg):
with app.app_context():
mail.send(msg)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through