From e94a2986ba04dc06303181114d8093ec2d47724f Mon Sep 17 00:00:00 2001 From: rintisch Date: Tue, 26 Mar 2024 16:42:31 +0100 Subject: [PATCH 1/2] [TASK] Update code in MailHandler Set base for further code changes. --- Classes/Service/MailHandler.php | 327 ++++++++++++-------------------- 1 file changed, 119 insertions(+), 208 deletions(-) diff --git a/Classes/Service/MailHandler.php b/Classes/Service/MailHandler.php index 6e745f0b..90d03c95 100644 --- a/Classes/Service/MailHandler.php +++ b/Classes/Service/MailHandler.php @@ -15,85 +15,39 @@ use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Mail\FluidEmail; -use TYPO3\CMS\Core\Mail\Mailer; +use TYPO3\CMS\Core\Mail\MailerInterface; use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; class MailHandler implements SingletonInterface { - /** - * @var LogManager - */ - protected $logManager; - - /** - * @var ConfigurationManager - */ - protected $configurationManager; - - /** - * @var array - */ - protected $pluginSettings = []; - - /** - * @var Cart - */ - protected $cart; - - /** - * @var string - */ - protected $buyerEmailFrom = ''; - - /** - * @var string - */ - protected $buyerEmailCc = ''; - - /** - * @var string - */ - protected $buyerEmailBcc = ''; - - /** - * @var string - */ - protected $buyerEmailReplyTo = ''; - - /** - * @var string - */ - protected $sellerEmailFrom = ''; - - /** - * @var string - */ - protected $sellerEmailTo = ''; - - /** - * @var string - */ - protected $sellerEmailCc = ''; - - /** - * @var string - */ - protected $sellerEmailBcc = ''; + protected LogManager $logManager; + protected ConfigurationManager $configurationManager; + private MailerInterface $mailer; + protected array $pluginSettings = []; + protected Cart $cart; + protected string $buyerEmailFrom = ''; + protected string $buyerEmailCc = ''; + protected string $buyerEmailBcc = ''; + protected string $buyerEmailReplyTo = ''; + protected string $sellerEmailFrom = ''; + protected string $sellerEmailTo = ''; + protected string $sellerEmailCc = ''; + protected string $sellerEmailBcc = ''; /** * MailHandler constructor */ - public function __construct() + public function __construct( + LogManager $logManager, + ConfigurationManager $configurationManager, + MailerInterface $mailer + ) { - $this->logManager = GeneralUtility::makeInstance( - LogManager::class - ); - - $this->configurationManager = GeneralUtility::makeInstance( - ConfigurationManager::class - ); + $this->logManager = $logManager; + $this->configurationManager = $configurationManager; + $this->mailer = $mailer; $this->setPluginSettings(); } @@ -109,228 +63,187 @@ public function setPluginSettings(): void 'Cart' ); - if (!empty($this->pluginSettings['settings'])) { - if (!empty($this->pluginSettings['settings']['buyer']) - && !empty($this->pluginSettings['settings']['buyer']['emailFromAddress']) - ) { - $this->setBuyerEmailFrom($this->pluginSettings['settings']['buyer']['emailFromAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['buyer']) - && !empty($this->pluginSettings['mail']['buyer']['fromAddress']) - ) { - $this->setBuyerEmailFrom($this->pluginSettings['mail']['buyer']['fromAddress']); - } + if (empty($this->pluginSettings['settings'])) { + return; + } - if (!empty($this->pluginSettings['settings']['buyer']) - && !empty($this->pluginSettings['settings']['buyer']['emailCcAddress']) - ) { - $this->setBuyerEmailCc($this->pluginSettings['settings']['buyer']['emailCcAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['buyer']) - && !empty($this->pluginSettings['mail']['buyer']['ccAddress']) - ) { - $this->setBuyerEmailCc($this->pluginSettings['mail']['buyer']['ccAddress']); - } + // setBuyerEmailFrom + if (!empty($this->pluginSettings['settings']['buyer']) + && !empty($this->pluginSettings['settings']['buyer']['emailFromAddress']) + ) { + $this->setBuyerEmailFrom($this->pluginSettings['settings']['buyer']['emailFromAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['buyer']) + && !empty($this->pluginSettings['mail']['buyer']['fromAddress']) + ) { + $this->setBuyerEmailFrom($this->pluginSettings['mail']['buyer']['fromAddress']); + } - if (!empty($this->pluginSettings['settings']['buyer']) - && !empty($this->pluginSettings['settings']['buyer']['emailBccAddress']) - ) { - $this->setBuyerEmailBcc($this->pluginSettings['settings']['buyer']['emailBccAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['buyer']) - && !empty($this->pluginSettings['mail']['buyer']['bccAddress']) - ) { - $this->setBuyerEmailBcc($this->pluginSettings['mail']['buyer']['bccAddress']); - } + // setBuyerEmailCc + if (!empty($this->pluginSettings['settings']['buyer']) + && !empty($this->pluginSettings['settings']['buyer']['emailCcAddress']) + ) { + $this->setBuyerEmailCc($this->pluginSettings['settings']['buyer']['emailCcAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['buyer']) + && !empty($this->pluginSettings['mail']['buyer']['ccAddress']) + ) { + $this->setBuyerEmailCc($this->pluginSettings['mail']['buyer']['ccAddress']); + } - if (!empty($this->pluginSettings['settings']['buyer']) - && !empty($this->pluginSettings['settings']['buyer']['emailReplyToAddress']) - ) { - $this->setBuyerEmailReplyTo($this->pluginSettings['settings']['buyer']['emailReplyToAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['buyer']) - && !empty($this->pluginSettings['mail']['buyer']['replyToAddress']) - ) { - $this->setBuyerEmailReplyTo($this->pluginSettings['mail']['buyer']['replyToAddress']); - } + // setBuyerEmailBcc + if (!empty($this->pluginSettings['settings']['buyer']) + && !empty($this->pluginSettings['settings']['buyer']['emailBccAddress']) + ) { + $this->setBuyerEmailBcc($this->pluginSettings['settings']['buyer']['emailBccAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['buyer']) + && !empty($this->pluginSettings['mail']['buyer']['bccAddress']) + ) { + $this->setBuyerEmailBcc($this->pluginSettings['mail']['buyer']['bccAddress']); + } - if (!empty($this->pluginSettings['settings']['seller']) - && !empty($this->pluginSettings['settings']['seller']['emailFromAddress']) - ) { - $this->setSellerEmailFrom($this->pluginSettings['settings']['seller']['emailFromAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['seller']) - && !empty($this->pluginSettings['mail']['seller']['fromAddress']) - ) { - $this->setSellerEmailFrom($this->pluginSettings['mail']['seller']['fromAddress']); - } + // setBuyerEmailReplyTo + if (!empty($this->pluginSettings['settings']['buyer']) + && !empty($this->pluginSettings['settings']['buyer']['emailReplyToAddress']) + ) { + $this->setBuyerEmailReplyTo($this->pluginSettings['settings']['buyer']['emailReplyToAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['buyer']) + && !empty($this->pluginSettings['mail']['buyer']['replyToAddress']) + ) { + $this->setBuyerEmailReplyTo($this->pluginSettings['mail']['buyer']['replyToAddress']); + } - if (!empty($this->pluginSettings['settings']['seller']) - && !empty($this->pluginSettings['settings']['seller']['emailToAddress']) - ) { - $this->setSellerEmailTo($this->pluginSettings['settings']['seller']['emailToAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['seller']) - && !empty($this->pluginSettings['mail']['seller']['toAddress']) - ) { - $this->setSellerEmailTo($this->pluginSettings['mail']['seller']['toAddress']); - } + // setSellerEmailFrom + if (!empty($this->pluginSettings['settings']['seller']) + && !empty($this->pluginSettings['settings']['seller']['emailFromAddress']) + ) { + $this->setSellerEmailFrom($this->pluginSettings['settings']['seller']['emailFromAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['seller']) + && !empty($this->pluginSettings['mail']['seller']['fromAddress']) + ) { + $this->setSellerEmailFrom($this->pluginSettings['mail']['seller']['fromAddress']); + } - if (!empty($this->pluginSettings['settings']['seller']) - && !empty($this->pluginSettings['settings']['seller']['emailCcAddress']) - ) { - $this->setSellerEmailCc($this->pluginSettings['settings']['seller']['emailCcAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['seller']) - && !empty($this->pluginSettings['mail']['seller']['ccAddress']) - ) { - $this->setSellerEmailCc($this->pluginSettings['mail']['seller']['ccAddress']); - } + // setSellerEmailTo + if (!empty($this->pluginSettings['settings']['seller']) + && !empty($this->pluginSettings['settings']['seller']['emailToAddress']) + ) { + $this->setSellerEmailTo($this->pluginSettings['settings']['seller']['emailToAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['seller']) + && !empty($this->pluginSettings['mail']['seller']['toAddress']) + ) { + $this->setSellerEmailTo($this->pluginSettings['mail']['seller']['toAddress']); + } - if (!empty($this->pluginSettings['settings']['seller']) - && !empty($this->pluginSettings['settings']['seller']['emailBccAddress']) - ) { - $this->setSellerEmailBcc($this->pluginSettings['settings']['seller']['emailBccAddress']); - } elseif (!empty($this->pluginSettings['mail']) - && !empty($this->pluginSettings['mail']['seller']) - && !empty($this->pluginSettings['mail']['seller']['bccAddress']) - ) { - $this->setSellerEmailBcc($this->pluginSettings['mail']['seller']['bccAddress']); - } + // setSellerEmailCc + if (!empty($this->pluginSettings['settings']['seller']) + && !empty($this->pluginSettings['settings']['seller']['emailCcAddress']) + ) { + $this->setSellerEmailCc($this->pluginSettings['settings']['seller']['emailCcAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['seller']) + && !empty($this->pluginSettings['mail']['seller']['ccAddress']) + ) { + $this->setSellerEmailCc($this->pluginSettings['mail']['seller']['ccAddress']); + } + + // setSellerEmailBcc + if (!empty($this->pluginSettings['settings']['seller']) + && !empty($this->pluginSettings['settings']['seller']['emailBccAddress']) + ) { + $this->setSellerEmailBcc($this->pluginSettings['settings']['seller']['emailBccAddress']); + } elseif (!empty($this->pluginSettings['mail']) + && !empty($this->pluginSettings['mail']['seller']) + && !empty($this->pluginSettings['mail']['seller']['bccAddress']) + ) { + $this->setSellerEmailBcc($this->pluginSettings['mail']['seller']['bccAddress']); } } - /** - * @param Cart $cart - */ public function setCart(Cart $cart): void { $this->cart = $cart; } - /** - * @param string $email - */ public function setBuyerEmailFrom(string $email): void { $this->buyerEmailFrom = $email; } - /** - * @return string - */ public function getBuyerEmailFrom(): string { return $this->buyerEmailFrom; } - /** - * @param string $buyerEmailCc - */ public function setBuyerEmailCc(string $buyerEmailCc): void { $this->buyerEmailCc = $buyerEmailCc; } - /** - * @return string - */ public function getBuyerEmailCc(): string { return $this->buyerEmailCc; } - /** - * @param string $buyerEmailBcc - */ public function setBuyerEmailBcc(string $buyerEmailBcc): void { $this->buyerEmailBcc = $buyerEmailBcc; } - /** - * @return string - */ public function getBuyerEmailBcc(): string { return $this->buyerEmailBcc; } - /** - * @param string $buyerEmailReplyTo - */ public function setBuyerEmailReplyTo(string $buyerEmailReplyTo): void { $this->buyerEmailReplyTo = $buyerEmailReplyTo; } - /** - * @return string - */ public function getBuyerEmailReplyTo(): string { return $this->buyerEmailReplyTo; } - /** - * @param string $email - */ public function setSellerEmailFrom(string $email): void { $this->sellerEmailFrom = $email; } - /** - * @return string - */ public function getSellerEmailFrom(): string { return $this->sellerEmailFrom; } - /** - * @param string $email - */ public function setSellerEmailTo(string $email): void { $this->sellerEmailTo = $email; } - /** - * @return string - */ public function getSellerEmailTo(): string { return $this->sellerEmailTo; } - /** - * @param string $sellerEmailCc - */ public function setSellerEmailCc(string $sellerEmailCc): void { $this->sellerEmailCc = $sellerEmailCc; } - /** - * @return string - */ public function getSellerEmailCc(): string { return $this->sellerEmailCc; } - /** - * @param string $sellerEmailBcc - */ public function setSellerEmailBcc(string $sellerEmailBcc): void { $this->sellerEmailBcc = $sellerEmailBcc; } - /** - * @return string - */ public function getSellerEmailBcc(): string { return $this->sellerEmailBcc; @@ -338,8 +251,6 @@ public function getSellerEmailBcc(): string /** * Send a Mail to Buyer - * - * @param Item $orderItem */ public function sendBuyerMail(Item $orderItem): void { @@ -349,7 +260,8 @@ public function sendBuyerMail(Item $orderItem): void $status = $orderItem->getPayment()->getStatus(); - $email = GeneralUtility::makeInstance(FluidEmail::class) + $email = new FluidEmail(); + $email ->to($orderItem->getBillingAddress()->getEmail()) ->from($this->getBuyerEmailFrom()) ->setTemplate('Mail/' . ucfirst($status) . '/Buyer') @@ -388,13 +300,11 @@ public function sendBuyerMail(Item $orderItem): void $email->setRequest($GLOBALS['TYPO3_REQUEST']); } - GeneralUtility::makeInstance(Mailer::class)->send($email); + $this->mailer->send($email); } /** * Send a Mail to Seller - * - * @param Item $orderItem */ public function sendSellerMail(Item $orderItem): void { @@ -406,7 +316,8 @@ public function sendSellerMail(Item $orderItem): void $status = $orderItem->getPayment()->getStatus(); $to = explode(',', $sellerEmailTo); - $email = GeneralUtility::makeInstance(FluidEmail::class) + $email = new FluidEmail(); + $email ->to(...$to) ->from($this->getSellerEmailFrom()) ->setTemplate('Mail/' . ucfirst($status) . '/Seller') @@ -446,6 +357,6 @@ public function sendSellerMail(Item $orderItem): void $email->setRequest($GLOBALS['TYPO3_REQUEST']); } - GeneralUtility::makeInstance(Mailer::class)->send($email); + $this->mailer->send($email); } } From 65a8e6c923911c993b99927750076acd3dc64cf2 Mon Sep 17 00:00:00 2001 From: rintisch Date: Tue, 26 Mar 2024 17:12:55 +0100 Subject: [PATCH 2/2] [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 ++ ...ure-301-AllowNamesInFromEmailAddresses.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-AllowNamesInFromEmailAddresses.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 + + +