2012-01-06 73 views
0

我试图从本地主机发送激活码给用户邮件(当前gmail)..当提交用户信息保存在数据库中,但邮件没有发送..所以为什么不发送?为什么电子邮件组件不会向gmail发送激活码?

var $components = array('Email','Auth','Recaptcha'); 
// Allows a user to sign up for a new account 
    function register() { 
     if (!empty($this->data)) { 
      // See my previous post if this is forgien to you 
    if($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])){ 
       $this->User->data = Sanitize::clean($this->data); 
      // Successfully created account - send activation email 
     if($this->Recaptcha->valid($this->params['form'])){ 
      if ($this->User->save()) { 
       $this->__sendActivationEmail($this->User->getLastInsertID()); 
       $this->Session->setFlash('activation code sent check your mail'); 
       $this->redirect('/users/register'); 
      }else { 
      $this->data['User']['password'] = null; 
      } 

     }else{ 
      $this->data['User']['password'] = null; 
      $this->Session->setFlash('wrong captcha please try again'); 
     } 
    }else{ 
     $this->data['User']['password'] = null; 
     $this->Session->setFlash('password not match'); 
    } 

    } 
    } 

这个函数发送了一封激活邮件至$ USER_ID指定的user.id @param诠释$ user_ID的用户发送激活邮件至 @返回布尔表示成功

function __sendActivationEmail($user_id) { 
     $user = $this->User->find(array('User.id' => $user_id), array('User.id','User.email', 'User.username'), null, false); 
     if ($user === false) { 
      debug(__METHOD__." failed to retrieve User data for user.id: {$user_id}"); 
      return false; 
     } 

     // Set data for the "view" of the Email 

     $this->set('activate_url', 'http://' . env('SERVER_NAME') . '/cakenews/users/activate/' . $user['User']['id'] . '/' . $this->User->getActivationHash()); 
    $this->set('username', $this->data['User']['username']); 

     $this->Email->to = $user['User']['email']; 
     $this->Email->subject = env('SERVER_NAME') . ' - Please confirm your email address'; 
     $this->Email->from = '[email protected]'; 
     $this->Email->template = 'user_confirm'; 
     $this->Email->delivery = 'smtp'; 
     $this->Email->smtpOptions = array(
'port'=>'465', 
'timeout'=>'30', 
'host' => 'ssl://smtp.gmail.com', 
'username'=>'[email protected]', 
'password'=>1234567, 

       ); 
     $this->Email->sendAs = 'text'; // you probably want to use both :) 
     return $this->Email->send(); 
    } 

回答

2

你写你在本地主机,你可能无法发送电子邮件,但可能会在线上工作。

尝试调试

function __sendActivationEmail($user_id) { 
    $this->Email->delivery = 'debug'; 
    .... 
} 

然后在你的布局

<?php echo $this->Session->flash('email'); ?> 

,看看什么出来。