How create a simple function

I try created a function to send emails.

This functions works, but send me a error page.(send e-mail but redirect to error page)
Error: Call to undefined function App\Controller\mailerFunction()

In PagesController I have:

  public function depoimentos(){
        $query = $this->Testimonials->find('all');
        $results = $query->toArray();
        $this->set('testimonials', $results);

        if ($this->request->is('post')) {


  public function mailerFunction(){
        $contact = new ContactForm();
        $erros = [];
                if ($contact->execute($this->request->data)) {
                    $var = $this->request->getData();
                    $this->Flash->success('Mensagem enviado, aguarde nosso retorno.' ); //. $var['email'] . $var['subject'] 
                     $email = new Email();
                        $email->from(['' => ''])
                        ->subject("Assunto: ".$var['email'])
                        ->send(" Mensagem: ".$var['message']);

                } else {
                    $erros = $contact->errors();
                    $this->Flash->error('Ocorreu um problema ao enviar sua mensagem.');
        $this->set('contact', $contact);
      //  $this->set('erros',$erros);

      return true;

Can I call this function without redirect to mailerFunction page?

You can. You need to use $this->mailerFunction() though as you’re trying to call an instance method.