Skip to content

Commit

Permalink
Update OrderEmailNotifier.php (#829)
Browse files Browse the repository at this point in the history
Added logging to provide details about failed emails
  • Loading branch information
AntonyThorpe authored Jul 1, 2024
1 parent 19e2e29 commit 7e1b330
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions src/Checkout/OrderEmailNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverShop\Checkout;

use Psr\Log\LoggerInterface;
use SilverShop\Extension\ShopConfigExtension;
use SilverShop\Model\Order;
use SilverShop\Model\OrderStatusLog;
Expand All @@ -12,6 +13,7 @@
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;

/**
* Handles email notifications to customers and / or admins.
Expand Down Expand Up @@ -44,6 +46,15 @@ class OrderEmailNotifier
*/
private static $bcc_status_change_to_admin = false;

private static $dependencies = [
'Logger' => '%$' . LoggerInterface::class,
];

/**
* @var LoggerInterface
*/
protected $logger;

/**
* @var Order $order
*/
Expand Down Expand Up @@ -116,7 +127,7 @@ protected function buildEmail($template, $subject)
*
* @return bool|string
*/
public function sendEmail($template, $subject, $copyToAdmin = true)
public function sendEmail($template, $subject, $copyToAdmin = true): bool|string
{
$email = $this->buildEmail($template, $subject);

Expand All @@ -126,7 +137,13 @@ public function sendEmail($template, $subject, $copyToAdmin = true)
if ($this->debugMode) {
return $this->debug($email);
} else {
return $email->send();
try {
$email->send();
} catch (TransportExceptionInterface $e) {
$this->logger->error('OrderEmailNotifier.sendEmail: error sending email in ' . __FILE__ . ' line ' . __LINE__ . ": {$e->getMessage()}");
return false;
}
return true;
}
}

Expand Down Expand Up @@ -155,7 +172,7 @@ public function sendConfirmation()
*
* @return bool|string
*/
public function sendAdminNotification()
public function sendAdminNotification(): bool|string
{
$subject = _t(
'SilverShop\ShopEmail.AdminNotificationSubject',
Expand All @@ -170,7 +187,13 @@ public function sendAdminNotification()
if ($this->debugMode) {
return $this->debug($email);
} else {
return $email->send();
try {
$email->send();
} catch (TransportExceptionInterface $e) {
$this->logger->error('OrderEmailNotifier.sendAdminNotification: error sending email in ' . __FILE__ . ' line ' . __LINE__ . ": {$e->getMessage()}");
return false;
}
return true;
}
}

Expand All @@ -197,7 +220,7 @@ public function sendReceipt()
/**
* Sends an email to the admin that an order has been cancelled
*/
public function sendCancelNotification()
public function sendCancelNotification(): bool|string
{
$email = Email::create()
->setSubject(_t(
Expand All @@ -213,7 +236,13 @@ public function sendCancelNotification()
if ($this->debugMode) {
return $this->debug($email);
} else {
return $email->send();
try {
$email->send();
} catch (TransportExceptionInterface $e) {
$this->logger->error('OrderEmailNotifier.sendCancelNotification: error sending email in ' . __FILE__ . ' line ' . __LINE__ . ": {$e->getMessage()}");
return false;
}
return true;
}
}

Expand All @@ -225,7 +254,7 @@ public function sendCancelNotification()
*
* @return bool|string
*/
public function sendStatusChange($title, $note = null)
public function sendStatusChange($title, $note = null): bool|string
{
$latestLog = null;

Expand Down Expand Up @@ -272,7 +301,13 @@ public function sendStatusChange($title, $note = null)
if ($this->debugMode) {
$result = $this->debug($email);
} else {
$result = $email->send();
try {
$email->send();
} catch (TransportExceptionInterface $e) {
$this->logger->error('OrderEmailNotifier.sendStatusChange: error sending email in ' . __FILE__ . ' line ' . __LINE__ . ": {$e->getMessage()}");
return false;
}
return true;
}

if ($latestLog) {
Expand Down Expand Up @@ -304,4 +339,14 @@ protected function debug(Email $email)
"<h3>Body</h3>" .
$htmlRender;
}

/**
* @param LoggerInterface $logger
* @return $this
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
return $this;
}
}

0 comments on commit 7e1b330

Please sign in to comment.