Skip to content

Commit

Permalink
FIX Allow new password to save even if there's an error sending email
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Dec 13, 2023
1 parent bf45b0c commit 446810b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Security/Member.php
Expand Up @@ -4,6 +4,7 @@

use IntlDateFormatter;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Control\Director;
Expand Down Expand Up @@ -34,7 +35,9 @@
use SilverStripe\ORM\UnsavedRelationList;
use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\ValidationResult;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Exception\RfcComplianceException;

/**
* The member class which represents the users of the system
Expand Down Expand Up @@ -772,18 +775,24 @@ public function onBeforeWrite()
&& static::config()->get('notify_password_change')
&& $this->isInDB()
) {
$email = Email::create()
->setHTMLTemplate('SilverStripe\\Control\\Email\\ChangePasswordEmail')
->setData($this)
->setTo($this->Email)
->setSubject(_t(
__CLASS__ . '.SUBJECTPASSWORDCHANGED',
"Your password has been changed",
'Email subject'
));

$this->extend('updateChangedPasswordEmail', $email);
$email->send();
try {
$email = Email::create()
->setHTMLTemplate('SilverStripe\\Control\\Email\\ChangePasswordEmail')
->setData($this)
->setTo($this->Email)
->setSubject(_t(
__CLASS__ . '.SUBJECTPASSWORDCHANGED',
"Your password has been changed",
'Email subject'
));

$this->extend('updateChangedPasswordEmail', $email);
$email->send();
} catch (TransportExceptionInterface | RfcComplianceException $e) {
/** @var LoggerInterface $logger */
$logger = Injector::inst()->get(LoggerInterface::class . '.errorhandler');
$logger->error('Error sending email in ' . __FILE__ . ' line ' . __LINE__ . ": {$e->getMessage()}");
}
}

// The test on $this->ID is used for when records are initially created. Note that this only works with
Expand Down

0 comments on commit 446810b

Please sign in to comment.