我无法发送mail.with以下代码: - 如果我删除$ Email-> config('gmail'),我可以发送id lke上的电子邮件: - test@gmail.com但无法在test @ test-test @ gmail.com发送电子邮件
下面是我在AppController中添加的代码: -
function sendEmail($email, $subject, $body){
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$send=$Email->config('gmail')->from(array('support@test-apps.com'=>'test Apps'))
->to($email)
->subject($subject)
->emailFormat('html')
->send($body);
if($send){
return "sent";
}else {
return "error";
}
}
my email config is :-
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my@gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);
}
答案 0 :(得分:1)
你也没有设置配置。试试这个
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('tesst@gmail.com')
->to($to)
->subject($subject);
if($Email->send($body)){
return "sent";
}else {
return "error";
}
class EmailConfig {
public $smtp = array(
'transport' => 'Smtp',
'from' => array('info@domain.co.uk' => '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' => 'my@gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);
}
需要将$ Email-> config(' smtp')更改为$ Email-> config(' gmail')
并在当前控制器或AppController
之上加载App::uses('CakeEmail', 'Network/Email');
答案 1 :(得分:1)
https://book.cakephp.org/2.0/en/core-utility-libraries/email.html
App::uses('CakeEmail', 'Network/Email');
使用CakeEmail类似于使用EmailComponent。而不是 使用属性,您使用方法。例如:
$Email = new CakeEmail();
$Email->from(array('me@example.com' => 'My Site'));
$Email->to('you@example.com');
$Email->subject('About');
$Email->send('My message');