/**
* Send forgot password email.
*
* @param string $Email
* @return bool
*/
public function passwordRequest($Email)
{
if (!$Email) {
return false;
}
$Users = $this->getWhere(['Email' => $Email])->resultObject();
if (count($Users) == 0) {
// Check for the username.
$Users = $this->getWhere(['Name' => $Email])->resultObject();
}
$this->EventArguments['Users'] =& $Users;
$this->EventArguments['Email'] = $Email;
$this->fireEvent('BeforePasswordRequest');
if (count($Users) == 0) {
$this->Validation->addValidationResult('Name', "Couldn't find an account associated with that email/username.");
return false;
}
$NoEmail = true;
foreach ($Users as $User) {
if (!$User->Email) {
continue;
}
$Email = new Gdn_Email();
// Instantiate in loop to clear previous settings
$PasswordResetKey = betterRandomString(20, 'Aa0');
$PasswordResetExpires = strtotime('+1 hour');
$this->saveAttribute($User->UserID, 'PasswordResetKey', $PasswordResetKey);
$this->saveAttribute($User->UserID, 'PasswordResetExpires', $PasswordResetExpires);
$AppTitle = c('Garden.Title');
$Email->subject('[' . $AppTitle . '] ' . t('Reset Your Password'));
$Email->to($User->Email);
$emailTemplate = $Email->getEmailTemplate()->setTitle(t('Reset Your Password'))->setMessage(sprintf(t('We\'ve received a request to change your password.'), $AppTitle))->setButton(externalUrl('/entry/passwordreset/' . $User->UserID . '/' . $PasswordResetKey), t('Change My Password'));
$Email->setEmailTemplate($emailTemplate);
try {
$Email->send();
} catch (Exception $e) {
if (debug()) {
throw $e;
}
}
$NoEmail = false;
}
if ($NoEmail) {
$this->Validation->addValidationResult('Name', 'There is no email address associated with that account.');
return false;
}
return true;
}