From 7e1b3308e7d29766d4de6af21ff2babb0f31926e Mon Sep 17 00:00:00 2001 From: Antony Thorpe <1023740+AntonyThorpe@users.noreply.github.com> Date: Tue, 2 Jul 2024 08:33:27 +1200 Subject: [PATCH] Update OrderEmailNotifier.php (#829) Added logging to provide details about failed emails --- src/Checkout/OrderEmailNotifier.php | 61 +++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/src/Checkout/OrderEmailNotifier.php b/src/Checkout/OrderEmailNotifier.php index be228aba..ddda3a99 100644 --- a/src/Checkout/OrderEmailNotifier.php +++ b/src/Checkout/OrderEmailNotifier.php @@ -2,6 +2,7 @@ namespace SilverShop\Checkout; +use Psr\Log\LoggerInterface; use SilverShop\Extension\ShopConfigExtension; use SilverShop\Model\Order; use SilverShop\Model\OrderStatusLog; @@ -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. @@ -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 */ @@ -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); @@ -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; } } @@ -155,7 +172,7 @@ public function sendConfirmation() * * @return bool|string */ - public function sendAdminNotification() + public function sendAdminNotification(): bool|string { $subject = _t( 'SilverShop\ShopEmail.AdminNotificationSubject', @@ -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; } } @@ -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( @@ -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; } } @@ -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; @@ -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) { @@ -304,4 +339,14 @@ protected function debug(Email $email) "