Accessing $kirby inside route

i need to send an automatic email response after a user signs up through a form.

once i get the data in the route plugin through POST from the frontend, i was thinking it was possible to

try {
  $kirby->email([
    'from' => 'welcome@supercompany.com',
    'to' => 'someone@gmail.com',
    'subject' => 'Welcome!',
    'body'=> 'It\'s great to have you with us',
  ]);
} catch (Exception $error) {
  echo $error;
}

but $kirby is not accessible. i tried to set a callback to the route plugin like so

Kirby::plugin('your/plugin', [
  'routes' => function ($kirby) {
      return [
          [
              'pattern' => 'something',
              'action' => function () {

                try {
                  $kirby->email([
                    'from' => 'welcome@supercompany.com',
                    'to' => 'someone@gmail.com',
                    'subject' => 'Welcome!',
                    'body'=> 'It\'s great to have you with us',
                  ]);
               } catch (Exception $error) {
                 echo $error;
               }

            }
         ]
    ];
  }
]);

but not working as well. then i tried to set up a route:after hook, and the hook works once the route above gets visited, but i don’t know how to pass the data to it?

(i thought of making a GET call to the route where i send the form data, but i don’t think you can nest hooks within hooks right?)

i guess i can setup a controller to make the GET call and use the form data to send the email, as i imagine in the controller i can use the $kirby object.

but is there any better and simpler approach to this?

You should be able the Kirby object from anywhere using the kirby() helper:

$kirby = kirby();

Please choose your topic categories wisely…:wink: And in any case a category that fits your Kirby version. Thanks.

thanks! working…

i tried that also before, but instead of bounding kirby() to a variable, i used it directly in the function and it did not do much.

and yes, i opened this topic at the end of a long day, i’ll make sure to add those categories from now on! :smile: