How create a simple function


#1

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(){
        $this->loadModel('Testimonials');
        $query = $this->Testimonials->find('all');
        $results = $query->toArray();
        $this->set('testimonials', $results);

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

and

  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(['myemail@gmail.com' => 'teste.com.br'])
                        ->transport('gmail')
                        ->to('destinyemail@gmail.com')
                        ->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?


#2

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