Skip to content

EspoCRM – Open Source CRM Application

store sent email encoding fix

Yuri Kuznetsov47a9a0616038
diff --git a/application/Espo/Core/Mail/Account/GroupAccount/Service.php b/application/Espo/Core/Mail/Account/GroupAccount/Service.php
index c85c8a7864..814decfc2e 100644
--- a/application/Espo/Core/Mail/Account/GroupAccount/Service.php
+++ b/application/Espo/Core/Mail/Account/GroupAccount/Service.php
@@ -35,14 +35,14 @@ use Espo\Core\Exceptions\Error;
use Espo\Core\Mail\Account\Fetcher;
use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Account\StorageFactory;
-
use Espo\Core\Mail\Account\Util\NotificationHelper;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
+use Espo\Core\Mail\Sender\Message;
use Espo\Core\Utils\Log;
use Exception;
+
use Laminas\Mail\Exception\ExceptionInterface;
-use Laminas\Mail\Message;
class Service
{
diff --git a/application/Espo/Core/Mail/Account/PersonalAccount/Service.php b/application/Espo/Core/Mail/Account/PersonalAccount/Service.php
index 3ba29ad81d..8c01010474 100644
--- a/application/Espo/Core/Mail/Account/PersonalAccount/Service.php
+++ b/application/Espo/Core/Mail/Account/PersonalAccount/Service.php
@@ -34,10 +34,6 @@ use Espo\Core\Mail\Account\Util\NotificationHelper;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
use Espo\Core\Utils\Log;
-use Exception;
-use Laminas\Mail\Exception\ExceptionInterface;
-use Laminas\Mail\Message;
-
use Espo\Core\Mail\Account\Account as Account;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\Error;
@@ -45,6 +41,11 @@ use Espo\Core\Mail\Account\Fetcher;
use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Account\StorageFactory;
use Espo\Entities\User;
+use Espo\Core\Mail\Sender\Message;
+
+use Laminas\Mail\Exception\ExceptionInterface;
+
+use Exception;
class Service
{
diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php
index 8ba339875b..b30d54e7f5 100644
--- a/application/Espo/Core/Mail/Sender.php
+++ b/application/Espo/Core/Mail/Sender.php
@@ -31,6 +31,7 @@ namespace Espo\Core\Mail;
use Espo\Core\FileStorage\Manager as FileStorageManager;
use Espo\Core\Mail\Exceptions\NoSmtp;
+use Espo\Core\Mail\Sender\MessageContainer;
use Espo\Core\Mail\Sender\TransportPreparatorFactory;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\ORM\EntityCollection;
@@ -75,6 +76,7 @@ class Sender
private $attachmentList = null;
/** @var array{string, string}[] */
private array $headers = [];
+ private ?MessageContainer $messageContainer = null;
private const ATTACHMENT_ATTR_CONTENTS = 'contents';
@@ -98,6 +100,7 @@ class Sender
$this->attachmentList = null;
$this->overrideParams = [];
$this->headers = [];
+ $this->messageContainer = null;
}
/**
@@ -183,6 +186,17 @@ class Sender
return $this->setEnvelopeOptions($options);
}
+ /**
+ * @since 9.2.0
+ * @internal
+ */
+ public function withMessageContainer(MessageContainer $messageContainer): self
+ {
+ $this->messageContainer = $messageContainer;
+
+ return $this;
+ }
+
/**
* Set a message instance.
*
@@ -307,6 +321,10 @@ class Sender
$envelope = $this->prepareEnvelope($message);
+ if ($this->messageContainer) {
+ $this->messageContainer->message = new Sender\Message($message);
+ }
+
try {
$this->transport->send($message, $envelope);
} catch (Exception|TransportExceptionInterface $e) {
diff --git a/application/Espo/Core/Mail/Sender/Message.php b/application/Espo/Core/Mail/Sender/Message.php
new file mode 100644
index 0000000000..0f54a67674
--- /dev/null
+++ b/application/Espo/Core/Mail/Sender/Message.php
@@ -0,0 +1,48 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Mail\Sender;
+
+use Symfony\Component\Mime\Email;
+
+/**
+ * @since 9.2.0
+ * @internal
+ */
+class Message
+{
+ public function __construct(
+ private Email $email,
+ ) {}
+
+ public function toString(): string
+ {
+ return $this->email->toString();
+ }
+}
diff --git a/application/Espo/Core/Mail/Sender/MessageContainer.php b/application/Espo/Core/Mail/Sender/MessageContainer.php
new file mode 100644
index 0000000000..93c0b6beae
--- /dev/null
+++ b/application/Espo/Core/Mail/Sender/MessageContainer.php
@@ -0,0 +1,41 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Mail\Sender;
+
+/**
+ * @since 9.2.0
+ * @internal
+ */
+class MessageContainer
+{
+ public function __construct(
+ public ?Message $message = null,
+ ) {}
+}
diff --git a/application/Espo/Tools/Email/SendService.php b/application/Espo/Tools/Email/SendService.php
index da60151e0a..2a3f50291d 100644
--- a/application/Espo/Tools/Email/SendService.php
+++ b/application/Espo/Tools/Email/SendService.php
@@ -68,7 +68,6 @@ use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Tools\Stream\Service as StreamService;
use Exception;
-use Laminas\Mail\Message;
use LogicException;
use const FILTER_VALIDATE_EMAIL;
@@ -215,13 +214,13 @@ class SendService
$this->validateEmailAddresses($entity);
- $message = new Message();
+ $messageContainer = new Sender\MessageContainer();
if (
$groupAccount instanceof GroupAccount && $groupAccount->storeSentEmails() ||
$personalAccount instanceof PersonalAccount && $personalAccount->storeSentEmails()
) {
- $sender->withMessage($message);
+ $sender->withMessageContainer($messageContainer);
}
$this->applyReplied($entity, $sender);
@@ -263,7 +262,7 @@ class SendService
$this->entityManager->saveEntity($entity, [Email::SAVE_OPTION_IS_JUST_SENT => true]);
- $this->store($message, $groupAccount, $personalAccount);
+ $this->store($messageContainer, $groupAccount, $personalAccount);
if ($parent) {
$this->streamService->noteEmailSent($parent, $entity);
@@ -294,8 +293,18 @@ class SendService
return $params;
}
- private function store(Message $message, ?Account $groupAccount, ?Account $personalAccount): void
- {
+ private function store(
+ Sender\MessageContainer $messageContainer,
+ ?Account $groupAccount,
+ ?Account $personalAccount,
+ ): void {
+
+ $message = $messageContainer->message;
+
+ if (!$message) {
+ return;
+ }
+
if ($groupAccount instanceof GroupAccount && $groupAccount->storeSentEmails()) {
$id = $groupAccount->getId() ?? null;