Skip to content

Commit

Permalink
[TASK] Allow names in from email addresses
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rintisch committed Mar 26, 2024
1 parent e94a298 commit 824103a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
55 changes: 53 additions & 2 deletions Classes/Service/MailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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'])
Expand Down
20 changes: 20 additions & 0 deletions Configuration/FlexForms/CartPlugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
</TCEforms>
<type>array</type>
<el>
<settings.seller.emailFromName>
<TCEforms>
<label>
LLL:EXT:cart/Resources/Private/Language/locallang_db.xml:tx_cart.flexform.seller.email_from_name
</label>
<config>
<type>input</type>
</config>
</TCEforms>
</settings.seller.emailFromName>
<settings.seller.emailFromAddress>
<TCEforms>
<label>
Expand Down Expand Up @@ -52,6 +62,16 @@
</config>
</TCEforms>
</settings.seller.emailBccAddress>
<settings.buyer.emailFromName>
<TCEforms>
<label>
LLL:EXT:cart/Resources/Private/Language/locallang_db.xml:tx_cart.flexform.buyer.email_from_name
</label>
<config>
<type>input</type>
</config>
</TCEforms>
</settings.buyer.emailFromName>
<settings.buyer.emailFromAddress>
<TCEforms>
<label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ the backend via the plugin, but also configured via TypoScript.
plugin.tx_cart {
mail {
// Used for emails sent to the customer (=buyer)
buyer {
fromName = Your Brand name
fromAddress = [email protected]
ccAddress = [email protected], [email protected]
bccAddress = [email protected], [email protected]
Expand All @@ -21,7 +23,10 @@ the backend via the plugin, but also configured via TypoScript.
1 = EXT:sitepackage/Resources/Public/Files/AGB.pdf
}
}
// Used for emails sent to the shop owner (=seller)
seller {
fromName = Cart TYPO3 System
fromAddress = [email protected]
toAddress = [email protected], [email protected]
ccAddress = [email protected], [email protected]
Expand Down
48 changes: 48 additions & 0 deletions Documentation/Changelog/9.0/Feature-301-AllowEmailNames.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.. include:: ../../Includes.txt

=================================
Feature: #301 - Allow Email Names
=================================

See :issue:`301`

Description
===========

It is now possible to define names which will be used when sending emails.
These names will be shown to the customer and shop owner when receiving mails.
Before only the email address was shown.

Impact
======

Negative effects are not expected. No adding the names will just have the same
result as before.

As with all the other mail settings those in the plugin take precedence.


Migration
=========
See also :ref:`the documentation for email addresses <adminstration_order_mail>`.

.. code-block:: typoscript
:caption: EXT:sitepackage/Configuration/TypoScript/setup.typoscript
plugin.tx_cart {
mail {
// Used for emails sent to the customer (=buyer)
buyer {
fromName = Your Brand name
// the other settings as shown in the documentation
}
// Used for emails sent to the shop owner (=seller)
seller {
fromName = Cart TYPO3 System
// the other settings as shown in the documentation
}
}
}
.. index:: TypoScript, Frontend
6 changes: 6 additions & 0 deletions Resources/Private/Language/locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<trans-unit id="tx_cart.flexform.cartPageId">
<source>Cart Page Id</source>
</trans-unit>
<trans-unit id="tx_cart.flexform.seller.email_from_name">
<source>Sender email name for email to the Seller</source>
</trans-unit>
<trans-unit id="tx_cart.flexform.seller.email_from_address">
<source>Sender email address for email to the Seller</source>
</trans-unit>
Expand All @@ -58,6 +61,9 @@
<trans-unit id="tx_cart.flexform.seller.email_bcc_address.description">
<source>The recipients of e-mails sent to one or more e-mail addresses separated by commas and listed in the so-called BCC (Blind Carbon Copy) field will receive a copy of the sent e-mail without their address being visible to the other specified recipients. (Source: Wikipedia)</source>
</trans-unit>
<trans-unit id="tx_cart.flexform.buyer.email_from_name">
<source>Sender email name for email to the Buyer</source>
</trans-unit>
<trans-unit id="tx_cart.flexform.buyer.email_from_address">
<source>Sender email address for email to the Buyer</source>
</trans-unit>
Expand Down

0 comments on commit 824103a

Please sign in to comment.