title: Python Flask with celery out of application context
body: <p>I am building a website using python Flask.  Everything is going good and now I am trying to implement celery.  </p>
  
  <p>That was going good as well until I tried to send an email using flask-mail from celery.  Now I am getting an "working outside of application context" error.</p>
  
  <p>full traceback is</p>
  
  <pre><code>  Traceback (most recent call last):
    File "/usr/lib/python2.7/site-packages/celery/task/trace.py", line 228, in trace_task
      R = retval = fun(*args, **kwargs)
    File "/usr/lib/python2.7/site-packages/celery/task/trace.py", line 415, in __protected_call__
      return self.run(*args, **kwargs)
    File "/home/ryan/www/CG-Website/src/util/mail.py", line 28, in send_forgot_email
      msg = Message("Recover your Crusade Gaming Account")
    File "/usr/lib/python2.7/site-packages/flask_mail.py", line 178, in __init__
      sender = current_app.config.get("DEFAULT_MAIL_SENDER")
    File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 336, in __getattr__
      return getattr(self._get_current_object(), name)
    File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 295, in _get_current_object
      return self.__local()
    File "/usr/lib/python2.7/site-packages/flask/globals.py", line 26, in _find_app
      raise RuntimeError('working outside of application context')
  RuntimeError: working outside of application context
  </code></pre>
  
  <p>This is my mail function:</p>
  
  <pre><code>@celery.task
  def send_forgot_email(email, ref):
      global mail
      msg = Message("Recover your Crusade Gaming Account")
      msg.recipients = [email]
      msg.sender = "Crusade Gaming stuff@cg.com"
      msg.html = \
          """
          Hello Person,&lt;br/&gt;
  
          You have requested your password be reset. &lt;a href="{0}" &gt;Click here recover your account&lt;/a&gt; or copy and paste this link in to your browser: {0} &lt;br /&gt;
  
          If you did not request that your password be reset, please ignore this.
          """.format(url_for('account.forgot', ref=ref, _external=True))
      mail.send(msg)
  </code></pre>
  
  <p>This is my celery file:</p>
  
  <pre><code>from __future__ import absolute_import
  
  from celery import Celery
  
  celery = Celery('src.tasks',
                  broker='amqp://',
                  include=['src.util.mail'])
  
  
  if __name__ == "__main__":
      celery.start()
  </code></pre>