title: RQ Python "Working outside of application context"
body: <p>I'm trying to use Redis &amp; RQ to set the task of sending an email, however, the "RQ Worker" is returning runtime error while using the function to send emails outside q.enqueue works fine.</p>
  
  <ul>
  <li><p><strong>app/routes.py</strong></p>
  
  <pre><code>routes = Blueprint("routes", __name__)
  r = Redis()
  q = Queue(connection=r)
  
  def sendEmail_task(recipient, message):
      msg = Message("Test Email", sender=("Me", "shawkyelshazly2@gmail.com"), 
      recipients=[recipient])
      msg.body = message
      msg.send(mail)
  
  @routes.route("/send_email", methods=["POST", "GET"])
  def send_mail():
      if request.method == "POST":
          recipient = request.form.get('email')
          message = request.form.get('message')
          job = q.enqueue(sendEmail_task, recipient, message)
          return redirect(url_for("routes.email_sent"))
  
      return render_template("send_email.html")
  </code></pre></li>
  <li><p><strong>app/__init__.py</strong></p>
  
  <pre><code>mail = Mail()
  
  def create_app(config_class = Config):
      app = Flask(__name__)
      from app.routes import routes
      app.register_blueprint(routes)    
      app.config.from_object(Config)
  
      with app.app_context():
          mail.init_app(app)
  
      return app
  </code></pre></li>
  <li><p><strong>run.py</strong> </p></li>
  </ul>
  
  <blockquote>
    <p>Which is outside the app folder</p>
  </blockquote>
  
  <pre><code>    from app import create_app
  
      app = create_app()
  
      if __name__ == '__main__':
          app.run(debug=True)
  </code></pre>