Aparentemente o
$this->request->getData()
Não retorna os dados.
ContatoController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContatoForm;
class ContatoController extends AppController {
public function index() {
$contato = new ContatoForm();
if ($this->request->is('post')) {
if ($contato->execute($this->request->getData())) {
$this->Flash->success('Email enviado com sucesso.');
} else {
$this->Flash->error('Erro enviando email.');
}
}
/*
if ($this->request->is('get')) {
$contato->setData([
'name' => 'Emmanuel',
'email' => 'emmanuel.ferro@gmail.com'
]);
}
*/
$this->set('contato', $contato);
}
}
?>
ContatoForm.php
<?php
//ContatoForm.php
namespace App\Form;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
use Cake\Network\Email\Email;
class ContatoForm extends Form {
public function _buildSchema(Schema $schema): Schema {
return $schema->addField('nome', 'string')
->addField('email', ['type' => 'string'])
->addField('msg', ['type' => 'text']);
}
public function validationDefault(Validator $validator): Validator {
$validator->minLength('nome', 10)
->email('email');
return $validator;
}
protected function _execute(array $data): bool {
$email = new Email('gmail');
$email->to('emmanuel.ferro@gmail.com');
$email->subject('Contato feito pelo site');
$msg = "Contato feito pelo site <br>
Nome: {$data['nome']}<br>
Email: {$data['email']}<br>
Mensagem: {$data['msg']}<br>
";
return $email->send($msg);
}
/*
public function _buildValidator(Validator $validator): Validator {
$validator->add('msg', [
'minLength' => [
'rule' => ['minLength', 10],
'message' => 'A mensagem precisa ter pelo menos 10 caracteres'
]
]
);
$validator->notEmpty('nome');
$validator->notEmpty('email');
return $validator;
}
public function _execute(array $data) {
var_dump($data);
exit();
}
*/
}
?>
app.php
'EmailTransport' => [
'default' => [
...
],
'gmail' => [
'className' => SmtpTransport::class,
/*
* The keys host, port, timeout, username, password, client and tls
* are used in SMTP transports
*/
'host' => 'smtp.gmail.com',
'port' => 587,
'timeout' => 60,
/*
* It is recommended to set these options through your environment or app_local.php
*/
'username' => 'emmanuel.ferro@gmail.com',
'password' => '**********', (Coloquei o valor correto aqui
'tls' => true,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
'Email' => [
'default' => [
'...
],
'gmail' => [
'transport' => 'gmail',
'from' => 'emmanuel.ferro@gmail.com',
/*
* Will by default be set to config value of App.encoding, if that exists otherwise to UTF-8.
*/
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
],