Skip to content

Instantly share code, notes, and snippets.

@francisrod01
Created July 17, 2013 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisrod01/6020537 to your computer and use it in GitHub Desktop.
Save francisrod01/6020537 to your computer and use it in GitHub Desktop.
EmailComponent for CakePHP 2.3
O bug apresentado é esse:
Error: Call to a member function sendmail() on a non-object
<?php
// Config/email.php
/**
* Class EmailConfig
*
* @example http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
*/
class EmailConfig {
public $default = array(
'transport' => 'Mail',
'from' => 'you@localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('EMAIL_AQUI' => 'NOME_SITE'),
'host' => 'ssl://hw92.webservidor.net',
'port' => 465,
'timeout' => 30,
'username' => 'EMAIL_AQUI',
'password' => 'SENHA_AQUI',
'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
// Controlles/Component/MailSenderComponent.php
App::uses('CakeEmail', 'Network/Email');
Class MailSenderComponent extends Component {
public function sendmail($subject, $titleMail, $name, $email, $message) {
$Email = new CakeEmail();
$Email->config('smtp');
// template use View/Emails/html/contact.ctp AND View/Layouts/Emails/html/simple_contact
$Email->template('contact', 'simple_mail')
->emailFormat('html')
->addHeaders(array('Date' => microtime(true)))
->subject($subject)
->to(array($email => $name))
->message($message)
->send();
return $Email->smtpError ? false : true;
}
}
?>
<?php
// Controllers/ProfileController.php
...
if ($this->MailSender->sendmail($subject, $titleMail, $name, $email, $message)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment