2012-01-06 55 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'); ?> 

,看看什麼出來。