How to print field of block in controller?

I have contact form as block, in which I want to have field for email, so that editor can set, who will get webform submission. How to print that email in controller?

Thinking about this, but it tells “Call to undefined function controller()”

 <?php controller('default', [
   

   'sendemail' => $block->sendemail(),
   
 
 
  ] ) ?>

I don’t understand your question.

As the error says, there is no controller() function. Where are you using this piece of code? What is in your default controller.

Please state exactly what you are trying to do.

Still need this. I have a block with contact form. User can edit labels, and I want to add ability for user to write, to which email form should be sent.

And the question is, how can I take this value and print it in controller?

I know how to take value from site blueprint, it works like this:

$sendemail = $site->formemail();

...

                    $kirby->email([
                    'template' => 'contactform',
                    'from'     => 'no_reply@example.com',
                    'replyTo'  => $data['email'],
                    'to'       => "$sendemail",

But don’t understand, how to take it from block.

Is question clear now, could you help with this please?

No, still information missing.

To me, it seems this block can be about anywhere. So how would you know in your controller which block to use?

This block is used in template default.php, so I guess controller can take value of that field from page?

Well, you can filter all block by this one type:

$contactFormBlock = $page->blocks()->filterBy('type', 'contact')->first();
$emailReceiver = $contactFormBlock ? $contactFormBlock->to() : 'fallback value'; // whatever the field is called

Will only work if there is only one such block.

Thanks, this gives: The form could not be sent: “” is not a valid email address

Well, check what you get as $emailReceiver. Make sure you use the right field names, I only guessed something

Gives empty. Double checked field name, it’s right.

Ok, that line was nonsense

$contactFormBlock = $page->fieldname()->toBlocks()->filterBy('type', 'contact')->first();

Tried this:

$contactFormBlock = $page->myblocks()->toBlocks()->filterBy('type', 'contactform')->first();
$emailReceiver = $contactFormBlock ? $contactFormBlock->to() : 'test@example.com'; 

returns only fallback value.