Skip to content

EspoCRM – Open Source CRM Application

smtp custom transport preparator

Yuri Kuznetsov6f9ba6c7f606
diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php
index da6deacef1..5829531abc 100644
--- a/application/Espo/Core/Mail/Sender.php
+++ b/application/Espo/Core/Mail/Sender.php
@@ -31,7 +31,7 @@ namespace Espo\Core\Mail;
use Espo\Core\FileStorage\Manager as FileStorageManager;
use Espo\Core\Mail\Exceptions\NoSmtp;
-use Espo\Core\Mail\Sender\DefaultTransportPreparator;
+use Espo\Core\Mail\Sender\TransportPreparatorFactory;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\ORM\EntityCollection;
use Espo\Core\Field\DateTime;
@@ -84,7 +84,7 @@ class Sender
private SendingAccountProvider $accountProvider,
private FileStorageManager $fileStorageManager,
private ConfigDataProvider $configDataProvider,
- private DefaultTransportPreparator $transportPreparator,
+ private TransportPreparatorFactory $transportPreparatorFactory,
) {
$this->useGlobal();
}
@@ -250,7 +250,9 @@ class Sender
$smtpParams = SmtpParams::fromArray($params);
- $this->transport = $this->transportPreparator->prepare($smtpParams);
+ $preparator = $this->transportPreparatorFactory->create($smtpParams);
+
+ $this->transport = $preparator->prepare($smtpParams);
}
/**
diff --git a/application/Espo/Core/Mail/Sender/TransportPreparatorFactory.php b/application/Espo/Core/Mail/Sender/TransportPreparatorFactory.php
new file mode 100644
index 0000000000..1edfc09361
--- /dev/null
+++ b/application/Espo/Core/Mail/Sender/TransportPreparatorFactory.php
@@ -0,0 +1,47 @@
+<?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 Espo\Core\InjectableFactory;
+use Espo\Core\Mail\SmtpParams;
+
+class TransportPreparatorFactory
+{
+ public function __construct(
+ private InjectableFactory $injectableFactory,
+ ) {}
+
+ public function create(SmtpParams $smtpParams): TransportPreparator
+ {
+ $className = $smtpParams->getTransportPreparatorClassName() ?? DefaultTransportPreparator::class;
+
+ return $this->injectableFactory->create($className);
+ }
+}
diff --git a/application/Espo/Core/Mail/SmtpParams.php b/application/Espo/Core/Mail/SmtpParams.php
index 217507a827..62ab604c8b 100644
--- a/application/Espo/Core/Mail/SmtpParams.php
+++ b/application/Espo/Core/Mail/SmtpParams.php
@@ -29,6 +29,7 @@
namespace Espo\Core\Mail;
+use Espo\Core\Mail\Sender\TransportPreparator;
use RuntimeException;
/**
@@ -47,6 +48,8 @@ class SmtpParams
private ?string $username = null;
private ?string $password = null;
private ?string $security = null;
+ /** @var ?class-string<TransportPreparator> */
+ private ?string $transportPreparatorClassName = null;
public const AUTH_MECHANISM_LOGIN = 'login';
public const AUTH_MECHANISM_CRAMMD5 = 'crammd5';
@@ -246,4 +249,26 @@ class SmtpParams
return $obj;
}
+
+ /**
+ * @param ?class-string<TransportPreparator> $transportPreparatorClassName
+ * @since 9.1.0.
+ * @noinspection PhpUnused
+ */
+ public function withTransportPreparatorClassName(?string $transportPreparatorClassName): self
+ {
+ $obj = clone $this;
+ $obj->transportPreparatorClassName = $transportPreparatorClassName;
+
+ return $obj;
+ }
+
+ /**
+ * @return ?class-string<TransportPreparator>
+ * @since 9.1.0.
+ */
+ public function getTransportPreparatorClassName(): ?string
+ {
+ return $this->transportPreparatorClassName;
+ }
}