2014-10-09 163 views
0

当我试图使用Cakephp发送电子邮件时,我得到“未知的电子邮件配置'gmail'”错误,是因为我从本地主机(xampp)发送它?发送电子邮件时出错CakePHP

if($this->User->save($this->request->data)){ 
     $message='Click on the link below to complete registration '; 
     $confirmation_link='www.sitename.com/users/verify/t:'.$hash.'/n:'.$this->data['User']['username'].''; 
     App::uses('CakeEmail', 'Network/Email'); 
     $email = new CakeEmail('gmail'); 
     $email->Email->from = '[email protected]'; 
     $email->Email->to=$this->data['User']['email']; 
     $email->Email->subject = 'Confirm Registration'; 
     $email->Email->smtpOptions = array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => 465, 
     'username' => '[email protected]', 
     'password' => 'mypassword', 
     'transport' => 'Smtp' 
     ); 
     $email->send($message . " " . $confirmation_link); 
     $this->Session->setFlash(__('you should activate your account')); 
     } 
} 

回答

1

为了使用new CakeEmail('gmail')你在你的配置文件来配置gmail(/Config/email.php)如:

public $gmail = [ 
    'transport' => 'Mail', 
    'from' => '[email protected]', 
    //'charset' => 'utf-8', 
    //'headerCharset' => 'utf-8', 
]; 

http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

new CakeEmail('gmail')将读取你的gmail配置而且您不必在应用程序中配置它。

如果你做你的配置在应用程序中,也许你想用new CakeEmail();

+0

感谢这么多的人,没有更多的错误!但使用本地主机与cakephp发送电子邮件是可能的?我找不到有关该 – Exchanger13 2014-10-09 01:43:27

+0

的任何参考它真的取决于您的本地主机的环境。如果我们正确配置环境,则可以使用本地主机发送电子邮件。把它当作真正的服务器。当你直接在真正的服务器上开发时,它也是一个'本地主机',对吗? – kyo 2014-10-09 03:56:56