Skip to content

Instantly share code, notes, and snippets.

@hugofabricio
Created January 21, 2014 12:45
Show Gist options
  • Select an option

  • Save hugofabricio/8539342 to your computer and use it in GitHub Desktop.

Select an option

Save hugofabricio/8539342 to your computer and use it in GitHub Desktop.
//Controller
<?php
public function contact()
{
if ($this->request->is('post')):
if ($this->Contact->send($this->request->data)):
$this->Session->setFlash('<div class="text-center">Mensagem enviada com sucesso.</div>', false);
$this->redirect(array('controller' => 'pages', 'action' => 'contact'));
else:
$this->Session->setFlash('<div class="text-center">Verifique os erros encontrado no formulário de preenchimento.</div>', false);
endif;
endif;
}
//Model
<?php
/**
*
* ContactModel
*
*/
App::uses('AppModel', 'Model');
App::uses('CakeEmail', 'Network/Email');
class Contact extends AppModel {
/**
* Não usa tabela
*/
public $useTable = false;
/**
* Validações
*/
public $validate = array(
'name' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Campo obrigatório.'
)
),
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Campo obrigatório.'
),
'type' => array(
'rule' => array('email'),
'message' => 'E-mail inválido.'
)
),
'message' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Campo obrigatório.'
)
),
);
public function send($data) {
$this->set($data);
if ($this->validates()):
$d = $data['Contact'];
$subject = substr($d['message'], 0, 20);
$name = "{$d['name']}";
CakeEmail::deliver(
Configure::read('Application.contact_mail'),
"{$d['subject']}",
compact('d'),
array(
'emailFormat' => 'html',
'template' => '/Pages/email',
'from' => array($d['email'] => $name)
)
);
// Envia o email
return true;
else:
return false;
endif;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment