From 824103a6e86e8e76431a66808351c1e0a9142869 Mon Sep 17 00:00:00 2001 From: rintisch Date: Tue, 26 Mar 2024 17:12:55 +0100 Subject: [PATCH] [TASK] Allow names in from email addresses Administrator can set names for the "from" addresses in TypoScript. Those names will be used when sending mails after orders to buyer and seller. Fixes #301 --- Classes/Service/MailHandler.php | 55 ++++++++++++++++++- Configuration/FlexForms/CartPlugin.xml | 20 +++++++ .../Order/EMailAddresses/Index.rst | 5 ++ .../9.0/Feature-301-AllowEmailNames.rst | 48 ++++++++++++++++ Resources/Private/Language/locallang_db.xlf | 6 ++ 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 Documentation/Changelog/9.0/Feature-301-AllowEmailNames.rst diff --git a/Classes/Service/MailHandler.php b/Classes/Service/MailHandler.php index 90d03c95..fc6451be 100644 --- a/Classes/Service/MailHandler.php +++ b/Classes/Service/MailHandler.php @@ -13,6 +13,7 @@ use Extcode\Cart\Domain\Model\Order\Item; use Extcode\Cart\Hooks\MailAttachmentHookInterface; use Psr\Http\Message\ServerRequestInterface; +use Symfony\Component\Mime\Address; use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Mail\FluidEmail; use TYPO3\CMS\Core\Mail\MailerInterface; @@ -27,10 +28,12 @@ class MailHandler implements SingletonInterface private MailerInterface $mailer; protected array $pluginSettings = []; protected Cart $cart; + protected string $buyerEmailName = ''; protected string $buyerEmailFrom = ''; protected string $buyerEmailCc = ''; protected string $buyerEmailBcc = ''; protected string $buyerEmailReplyTo = ''; + protected string $sellerEmailName = ''; protected string $sellerEmailFrom = ''; protected string $sellerEmailTo = ''; protected string $sellerEmailCc = ''; @@ -67,6 +70,18 @@ public function setPluginSettings(): void return; } + // setBuyerEmailName + if (!empty($this->pluginSettings['settings']['buyer']) + && !empty($this->pluginSettings['settings']['buyer']['emailFromName']) + ) { + $this->setBuyerEmailName($this->pluginSettings['settings']['buyer']['emailFromName']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['buyer']) + && !empty($this->pluginSettings['mail']['buyer']['fromName']) + ) { + $this->setBuyerEmailName($this->pluginSettings['mail']['buyer']['fromName']); + } + // setBuyerEmailFrom if (!empty($this->pluginSettings['settings']['buyer']) && !empty($this->pluginSettings['settings']['buyer']['emailFromAddress']) @@ -115,6 +130,18 @@ public function setPluginSettings(): void $this->setBuyerEmailReplyTo($this->pluginSettings['mail']['buyer']['replyToAddress']); } + // setSellerEmailName + if (!empty($this->pluginSettings['settings']['seller']) + && !empty($this->pluginSettings['settings']['seller']['emailFromName']) + ) { + $this->setSellerEmailName($this->pluginSettings['settings']['seller']['emailFromName']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['seller']) + && !empty($this->pluginSettings['mail']['seller']['fromName']) + ) { + $this->setSellerEmailName($this->pluginSettings['mail']['seller']['fromName']); + } + // setSellerEmailFrom if (!empty($this->pluginSettings['settings']['seller']) && !empty($this->pluginSettings['settings']['seller']['emailFromAddress']) @@ -169,6 +196,16 @@ public function setCart(Cart $cart): void $this->cart = $cart; } + public function setBuyerEmailName(string $name): void + { + $this->buyerEmailName = $name; + } + + public function getBuyerEmailName(): string + { + return $this->buyerEmailName; + } + public function setBuyerEmailFrom(string $email): void { $this->buyerEmailFrom = $email; @@ -209,6 +246,16 @@ public function getBuyerEmailReplyTo(): string return $this->buyerEmailReplyTo; } + public function setSellerEmailName(string $name): void + { + $this->sellerEmailName = $name; + } + + public function getSellerEmailName(): string + { + return $this->sellerEmailName; + } + public function setSellerEmailFrom(string $email): void { $this->sellerEmailFrom = $email; @@ -260,10 +307,12 @@ public function sendBuyerMail(Item $orderItem): void $status = $orderItem->getPayment()->getStatus(); + $fromAddress = new Address($this->getBuyerEmailFrom(), $this->getBuyerEmailName()); + $email = new FluidEmail(); $email ->to($orderItem->getBillingAddress()->getEmail()) - ->from($this->getBuyerEmailFrom()) + ->from($fromAddress) ->setTemplate('Mail/' . ucfirst($status) . '/Buyer') ->format(FluidEmail::FORMAT_HTML) ->assign('settings', $this->pluginSettings['settings']) @@ -315,11 +364,13 @@ public function sendSellerMail(Item $orderItem): void $status = $orderItem->getPayment()->getStatus(); + $fromAddress = new Address($this->getSellerEmailFrom(), $this->getSellerEmailName()); + $to = explode(',', $sellerEmailTo); $email = new FluidEmail(); $email ->to(...$to) - ->from($this->getSellerEmailFrom()) + ->from($fromAddress) ->setTemplate('Mail/' . ucfirst($status) . '/Seller') ->format(FluidEmail::FORMAT_HTML) ->assign('settings', $this->pluginSettings['settings']) diff --git a/Configuration/FlexForms/CartPlugin.xml b/Configuration/FlexForms/CartPlugin.xml index 97be8b9f..1d447023 100644 --- a/Configuration/FlexForms/CartPlugin.xml +++ b/Configuration/FlexForms/CartPlugin.xml @@ -10,6 +10,16 @@ array + + + + + input + + + + + + + + input + + +