title: Email error in Yii framework
body: <p>Am quite new to Yii framework. I hit a problem with one of the existing Yii application.</p>
  
  <p>There is a “forgot password” feature there. It's working like, enter username and security question answer. If both are correct, the system will send one link to the user’s email for password reset. Below is the function:</p>
  
  <pre><code>public function actionCheckAnswer()
      {
          if(IS_AJAX &amp;&amp; isset($_POST['answer']) &amp;&amp; isset($_POST['username']))
          {
              $username=$_POST['username'];
              $answer=$_POST['answer'];
              $user=User::model()-&gt;findByAttributes(array('username'=&gt;$username));
              if($user!=null)
              {
                  $realAnswer = $user-&gt;secretAnswer;
                  if(strlen($realAnswer)&gt;0)
                  {
                      $profile=Profile::model()-&gt;findByAttributes(array('userId'=&gt;$user-&gt;id));
                      if($this-&gt;checkAnswerSpam($profile-&gt;id))
                      {
                          if(strtolower($realAnswer)==strtolower($answer))
                          {
                              Activity::log(22, null, $profile-&gt;id, -1);
                              $stamp=Activity::model()-&gt;getLogTime(null, $profile-&gt;id, -1, 22);
                              $hash=$profile-&gt;id.'_'.sha1($profile-&gt;id.$stamp);
                              $url=Yii::app()-&gt;createAbsoluteUrl('site/recover').'/'.$hash;
                                                          echo $url;
                              $this-&gt;sendPasswordRecoveryLink($profile-&gt;fullName, $profile-&gt;email, $url);
                              //echo '&lt;br /&gt;'.CHtml::link($url, $url).'&lt;br /&gt;';
                              echo 'Correct! A link to your password recovery form has been sent to your e-mail. The link expires in 1 hour.&lt;br /&gt;If you don\'t receive a mail, please check your spam folder.';
                          } else {
                              Activity::log(24, null, $profile-&gt;id, -1);
                              echo 'Sorry, that answer is not correct.';
                          }
                      } 
                  } else {
                      echo 'Sorry, you have not set a secret question answer.';
                  }
              } else {
                  echo 'No user "'.$username.'" found.';
              }
          }
      }
  </code></pre>
  
  <p>Currently, this function will not send out the email. I did some trouble shooting and found that <code>Activity::log(22, null, $profile-&gt;id, -1);</code> gave me an error. If I comment this line then it will send out the email with the password reset link, but it is always an expired link. Below is the log function: </p>
  
  <pre><code>public function log($action=0, $trainingId=null, $profileId=null, $piId=null)
      {
          if($profileId==null) $profileId=Yii::app()-&gt;user-&gt;profileId;
          if($piId==null) $piId=(isset(Yii::app()-&gt;user-&gt;piId))?Yii::app()-&gt;user-&gt;piId:0;
          $activity=new Activity;
          $activity-&gt;trainingId=$trainingId;
          $activity-&gt;profileId=$profileId;
          $activity-&gt;piId=$piId;
          $activity-&gt;action=$action;
          $activity-&gt;save();
      }
  </code></pre>
  
  <p>Below is the function to check the expiry limit.</p>
  
  <pre><code>public function getLogTime($trainingId, $profileId, $piId, $action)
      {
          $all = Activity::model()-&gt;findAllByAttributes(array(
              'trainingId'=&gt;$trainingId,
              'profileId'=&gt;$profileId,
              'piId'=&gt;$piId,
              'action'=&gt;$action,
          ));
          foreach($all as $single) $return = $single;
          return $return-&gt;timestamp;
      }
  
      public function checkRecoveryHash($hash)
      {
          $explode=explode('_', $hash);
          $stamp=$this-&gt;getLogTime(null, $explode[0], -1, 22);
          if(strlen($stamp)&gt;0)
          {
              $time=time();
              $stamptime=strtotime($stamp);
              $passed=$time-$stamptime;
              if($passed &lt; 720*720) //1 hour
                  return true;
              else
                  return false;
          }
      }
  </code></pre>
  
  <p>Am not sure which part and how to amend. Can anybody tell me what’s wrong?</p>