Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created April 11, 2014 15:45
Show Gist options
  • Save patrickmaciel/10479007 to your computer and use it in GitHub Desktop.
Save patrickmaciel/10479007 to your computer and use it in GitHub Desktop.
Envio de e-mails com CakePHP (exemplo prático)
<div id="contact-form">
<h1><?php echo __('Contact') ?></h1>
<p class="lead">Aguardo seu contato ansioamente! Não fique na dúvida, pergunte o que desejar.</p>
<div class="well">
<p class='text-form'><strong>*</strong>: Campos obrigatórios</p>
<?php echo $this->Form->create(null, array('url' => array('controller' => 'notifications', 'action' => 'contact'), 'class' => 'form-horizontal')) ?>
<?php echo $this->Form->input('name', array('label' => __('Name') . ' *', 'class' => 'input-xlarge')); ?>
<?php echo $this->Form->input('email', array('label' => __('E-mail') . ' *', 'class' => 'input-xlarge')); ?>
<?php echo $this->Form->input('message', array('label' => __('Message') . ' *', 'class' => 'input-xxlarge', 'type' => 'textarea', 'rows' => 3)); ?>
<?php echo $this->Form->submit(__('Send'), array('class' => 'btn btn-success')) ?>
<?php echo $this->Form->end() ?>
</div>
</div>
<?php
class EmailConfig {
public $default = array(
'transport' => 'Mail',
'from' => 'you@localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $gmail = array(
'transport' => 'Smtp',
'from' => array('[email protected]' => 'Your Name'),
'port'=>'465',
'timeout'=>'30',
'auth' => true,
'host' => 'ssl://smtp.gmail.com',
'username'=>'[email protected]',
'password'=>'YOUR_PASSWORD',
'client' => null,
'log' => false
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('site@localhost' => 'My Site'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $fast = array(
'from' => 'you@localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'Smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => true,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
<?php
App::uses('AppController', 'Controller');
/**
* Notifications Controller
*
* @property Notification $Notification
*/
class NotificationsController extends AppController {
/**
* sendContact method
*
* @param
* @return void
*/
public function contact() {
$this->set('title_for_layout', 'Contato');
if($this->request->is('post')) {
$this->Notification->set($this->request->data);
if($this->Notification->validates(array('fieldList' => array('name','email','message')))) {
$email = new CakeEmail();
$email->config('gmail')
->from(array('[email protected]' => 'Your Site'))
->to('[email protected]')
->subject('Your Site - Contato - ' . $this->request->data['Notification']['name'])
->emailFormat('html')
->template('default')
->viewVars(array(
'notificationType' => 'Contato',
'message' => $this->request->data['Notification']['message'],
'name' => $this->request->data['Notification']['name'],
'email' => $this->request->data['Notification']['email'],
'title_for_layout' => 'Contato'
))
->send();
$this->request->data = null;
$this->Session->setFlash(__('Contact submitted successfully.'), 'Flash/success');
} else {
$error = $this->Notification->validationErrors;
$this->set('error', $error);
$this->Session->setFlash(__('Fill all fields'), 'Flash/warning');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment