Error with $kirby->email

Hi

I am trying to send an email from a form in kirby 3 (after an upgrade from kirby 2). I have this code:

try {
  $kirby->email(['from' => 'contactform@sirihustvedt.net',
    'to' => 'mike@bigflannel.com',
    'bcc' => 'mike@bigflannel.com',
    'replyTo' => $data[ 'email' ],
    'subject' => 'New inquiry from sirihustvedt.net',
    'body' => addslashes( $body ),
  ]);
} catch (Exception $error) {
  $alert = $error;
}

I am getting an error:

Whoops\Exception\ErrorException: Undefined variable: kirby in /Users/mike/work/localhost/sirihustvedt/site/controllers/page.php:34 Stack trace: #0 /Users/mike/work/localhost/sirihustvedt/site/controllers/page.php(34): Whoops\Run->handleError(8, 'Undefined varia...', '/Users/mike/wor...', 34, Array) #1 [internal function]: Kirby\Cms\App->{closure}(Object(Kirby\Cms\Site), Object(Kirby\Cms\Pages), Object(Kirby\Cms\Page)) #2 /Users/mike/work/localhost/sirihustvedt/kirby/src/Toolkit/Controller.php(50): Closure->call(Object(Kirby\Cms\App), Object(Kirby\Cms\Site), Object(Kirby\Cms\Pages), Object(Kirby\Cms\Page)) #3 /Users/mike/work/localhost/sirihustvedt/kirby/src/Cms/App.php(460): Kirby\Toolkit\Controller->call(Object(Kirby\Cms\App), Array) #4 /Users/mike/work/localhost/sirihustvedt/kirby/src/Cms/Page.php(351): Kirby\Cms\App->controller('page', Array, 'html') #5 /Users/mike/work/localhost/sirihustvedt/kirby/src/Cms/Page.php(1067): Kirby\Cms\Page->controller(Array, 'html') #6 /Users/mike/work/localhost/sirihustvedt/kirby/src/Cms/App.php(804): Kirby\Cms\Page->render() #7 /Users/mike/work/localhost/sirihustvedt/kirby/src/Cms/App.php(1255): Kirby\Cms\App->io(Object(Kirby\Cms\Page)) #8 /Users/mike/work/localhost/sirihustvedt/index.php(5): Kirby\Cms\App->render() #9 {main}

I am stumped. The data seems valid, $kirby->email() seems to be the right code to send an email, the error message seems to be suggesting kirby is undefined (:man_shrugging:). I am running this code in a controller attached to a page. The form posts to the controller I think.

Any suggestions, thoughts, ideas, or anything greatly appreciated.

Regards
Mike

Exactly, what does your page.php controller look like? Or see docs how you set those variables: Controllers | Kirby CMS

On a side note: If you use three backticks on separate lines before and after a code block, your code comes out correctly as code block.

Thank you!

It looked like this:

return function ($site, $pages, $page) {

I amended it to be this:

return function ( $kirby, $site, $pages, $page) {

and all is well. :slightly_smiling_face:

Appreciate the side note too!

Thank you once again.

Regards
Mike