问题
I am not able to send mail.with below code:- if I remove $Email->config('gmail') than i am able to send email on id lke :- test@gmail.com but unable to send email on test@test-test@gmail.com
below is the code i have added in 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'
);
}
回答1:
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('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'
);
}
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
回答2:
https://book.cakephp.org/2.0/en/core-utility-libraries/email.html
App::uses('CakeEmail', 'Network/Email');
Using CakeEmail is similar to using EmailComponent. But instead of using attributes, you use methods. Example:
$Email = new CakeEmail();
$Email->from(array('me@example.com' => 'My Site'));
$Email->to('you@example.com');
$Email->subject('About');
$Email->send('My message');
来源:https://stackoverflow.com/questions/22552483/i-want-to-send-email-from-cakephp-2-x-in-company-domain-email-adress