Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

php - I Want to send email from cakephp 2.x in company domain email adress

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 :- [email protected] but unable to send email on test@[email protected]

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('[email protected]'=>'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' => '[email protected]',
    'password' => 'secret',
    'transport' => 'Smtp'
   );
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...