public function testSendEmailCorrectUser() { $model = new PasswordResetRequestForm(); $model->email = $this->user[0]['email']; $user = User::findOne(['passwordResetToken' => $this->user[0]['passwordResetToken']]); expect('email sent', $model->send())->true(); expect('user has valid token', $user->passwordResetToken)->notNull(); expect('message file exists', file_exists($this->getMessageFile()))->true(); $message = file_get_contents($this->getMessageFile()); expect('message "from" is correct', $message)->contains(Yii::$app->params['appEmail']); expect('message "to" is correct', $message)->contains($model->email); }
/** * Sends an email with a link, for resetting the password. * * @return boolean whether the email was send */ public function send() { /* @var $user User */ $user = User::findOne(['statusId' => User::STATUS_ACTIVE, 'email' => $this->email]); if ($user) { if (!User::isPasswordResetTokenValid($user->passwordResetToken)) { $user->generatePasswordResetToken(); } if ($user->save()) { return Yii::$app->mailer->compose('passwordResetToken', ['user' => $user])->setFrom([Yii::$app->params['appEmail'] => Yii::$app->name])->setTo($this->email)->setSubject('Password reset for ' . Yii::$app->name)->send(); } } return false; }