you didn't set the config also. try this
function sendEmail($to, $subject, $body){
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->config('gmail') // gmail, smtp or any config you have created in email config
->emailFormat('html')
->from('[email protected]')
->to($to)
->subject($subject);
if($Email->send($body)){
return "sent";
}else {
return "error";
}
class EmailConfig {
public $smtp = array(
'transport' => 'Smtp',
'from' => array('[email protected]' => 'Company Name'),
'host' => 'company host',
'port' => 25,
'timeout' => 30,
'username' => 'email',
'password' => 'password',
'client' => null,
'log' => false,
);
//add other email config e.g. gmail
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => '[email protected]',
'password' => 'secret',
'transport' => 'Smtp'
);
}
jut need to change $Email->config('smtp') to $Email->config('gmail')
and load App::uses('CakeEmail', 'Network/Email');
on top of current controller or AppController
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…