Skip to content

EspoCRM – Open Source CRM Application

transport preparator

Yuri Kuznetsovd57cea662d4a
diff --git a/application/Espo/Binding.php b/application/Espo/Binding.php
index 03df41f07a..c38e616d07 100644
--- a/application/Espo/Binding.php
+++ b/application/Espo/Binding.php
@@ -312,6 +312,11 @@ class Binding implements BindingProcessor
'Espo\\Core\\Mail\\Importer',
'Espo\\Core\\Mail\\Importer\\DefaultImporter'
);
+
+ $binder->bindImplementation(
+ 'Espo\\Core\\Mail\\Sender\\TransportPreparator',
+ 'Espo\\Core\\Mail\\Sender\\DefaultTransportPreparator'
+ );
}
private function bindAcl(Binder $binder): void
diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php
index 490c18606d..d771b842a7 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\TransportPreparator;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\ORM\EntityCollection;
use Espo\Core\Field\DateTime;
@@ -45,22 +46,15 @@ use Espo\ORM\EntityManager;
use Laminas\Mail\Headers;
use Laminas\Mail\Message as LaminasMessage;
-use LogicException;
-use RuntimeException;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
-use Symfony\Component\Mailer\Transport\Dsn;
-use Symfony\Component\Mailer\Transport\Smtp\Auth\CramMd5Authenticator;
-use Symfony\Component\Mailer\Transport\Smtp\Auth\LoginAuthenticator;
-use Symfony\Component\Mailer\Transport\Smtp\Auth\PlainAuthenticator;
-use Symfony\Component\Mailer\Transport\Smtp\Auth\XOAuth2Authenticator;
-use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
-use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
+use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email as Message;
use Symfony\Component\Mime\Part\DataPart;
use Exception;
+use LogicException;
use InvalidArgumentException;
/**
@@ -68,7 +62,7 @@ use InvalidArgumentException;
*/
class Sender
{
- private ?EsmtpTransport $transport = null;
+ private ?TransportInterface $transport = null;
private bool $isGlobal = false;
/** @var array<string, mixed> */
private array $params = [];
@@ -90,6 +84,7 @@ class Sender
private SendingAccountProvider $accountProvider,
private FileStorageManager $fileStorageManager,
private ConfigDataProvider $configDataProvider,
+ private TransportPreparator $transportPreparator,
) {
$this->useGlobal();
}
@@ -253,82 +248,9 @@ class Sender
{
$this->params = $params;
- $config = $this->config;
-
- $localHostName = $config->get('smtpLocalHostName', gethostname());
-
- $security = strtolower($params['security'] ?? '');
-
- // SSL is treated as implicit SSL/TLS. TLS is treated as STARTTLS.
- // STARTTLS is the most common method.
- $scheme = $security == 'ssl' ? 'smtps' : 'smtp';
-
- if ($security === 'starttls' && !defined('OPENSSL_VERSION_NUMBER')) {
- throw new RuntimeException("OpenSSL is not available.");
- }
-
- // @todo Use `auto_tls=false` if no security when Symfony v7.1 is installed.
- // @todo If starttls, it should be enforced.
-
- $transport = (new EsmtpTransportFactory())
- ->create(
- new Dsn(
- scheme: $scheme,
- host: $params['server'],
- port: $params['port'],
- )
- );
-
- if (!$transport instanceof EsmtpTransport) {
- throw new RuntimeException();
- }
-
- $this->transport = $transport;
-
- $this->transport->setLocalDomain($localHostName);
+ $smtpParams = SmtpParams::fromArray($params);
- $authMechanism = null;
-
- $connectionOptions = $params['connectionOptions'] ?? [];
- $authString = $connectionOptions['authString'] ?? null;
-
- if ($authString) {
- $decodedAuthString = base64_decode($authString);
-
- /** @noinspection RegExpRedundantEscape */
- if (preg_match("/user=(.*?)\\\1auth=Bearer (.*?)\\\1\\\1/", $decodedAuthString, $matches) !== false) {
- $username = $matches[1];
- $token = $matches[2];
-
- $this->transport->setUsername($username);
- $this->transport->setPassword($token);
- }
-
- $authMechanism = 'xoauth';
- } else if ($params['auth'] ?? false) {
- $authMechanism = ($params['authMechanism'] ?? $params['smtpAuthMechanism']) ?: 'login';
-
- $this->transport->setUsername($params['username'] ?? '');
- $this->transport->setPassword($params['password'] ?? '');
- }
-
- if ($authMechanism === 'login') {
- $this->transport->setAuthenticators([new LoginAuthenticator()]);
- } else if ($authMechanism === 'crammd5') {
- $this->transport->setAuthenticators([new CramMd5Authenticator()]);
- } else if ($authMechanism === 'plain') {
- $this->transport->setAuthenticators([new PlainAuthenticator()]);
- } else if ($authMechanism === 'xoauth') {
- $this->transport->setAuthenticators([new XOAuth2Authenticator()]);
- }
-
- if (array_key_exists('fromName', $params)) {
- $this->params['fromName'] = $params['fromName'];
- }
-
- if (array_key_exists('fromAddress', $params)) {
- $this->params['fromAddress'] = $params['fromAddress'];
- }
+ $this->transport = $this->transportPreparator->prepare($smtpParams);
}
/**
diff --git a/application/Espo/Core/Mail/Sender/DefaultTransportPreparator.php b/application/Espo/Core/Mail/Sender/DefaultTransportPreparator.php
new file mode 100644
index 0000000000..5e98283c51
--- /dev/null
+++ b/application/Espo/Core/Mail/Sender/DefaultTransportPreparator.php
@@ -0,0 +1,117 @@
+<?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\Mail\SmtpParams;
+use Espo\Core\Utils\Config;
+use RuntimeException;
+use Symfony\Component\Mailer\Transport\Dsn;
+use Symfony\Component\Mailer\Transport\Smtp\Auth\CramMd5Authenticator;
+use Symfony\Component\Mailer\Transport\Smtp\Auth\LoginAuthenticator;
+use Symfony\Component\Mailer\Transport\Smtp\Auth\PlainAuthenticator;
+use Symfony\Component\Mailer\Transport\Smtp\Auth\XOAuth2Authenticator;
+use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
+use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
+use Symfony\Component\Mailer\Transport\TransportInterface;
+
+class DefaultTransportPreparator implements TransportPreparator
+{
+ public function __construct(
+ private Config $config
+ ) {}
+
+ public function prepare(SmtpParams $smtpParams): TransportInterface
+ {
+ $localHostName = $this->config->get('smtpLocalHostName', gethostname());
+
+ // 'SSL' is treated as implicit SSL/TLS. 'TLS' is treated as STARTTLS.
+ // STARTTLS is the most common method.
+ $scheme = $smtpParams->getSecurity() === 'SSL' ? 'smtps' : 'smtp';
+
+ if ($smtpParams->getSecurity() === 'TLS' && !defined('OPENSSL_VERSION_NUMBER')) {
+ throw new RuntimeException("OpenSSL is not available.");
+ }
+
+ // @todo Use `auto_tls=false` if no security when Symfony v7.1 is installed.
+ // @todo If starttls, it should be enforced.
+
+ $transport = (new EsmtpTransportFactory())
+ ->create(
+ new Dsn(
+ scheme: $scheme,
+ host: $smtpParams->getServer(),
+ port: $smtpParams->getPort(),
+ )
+ );
+
+ if (!$transport instanceof EsmtpTransport) {
+ throw new RuntimeException();
+ }
+
+ $transport->setLocalDomain($localHostName);
+
+ $authMechanism = null;
+
+ $connectionOptions = $smtpParams->getConnectionOptions() ?? [];
+ $authString = $connectionOptions['authString'] ?? null;
+
+ if ($authString) {
+ $decodedAuthString = base64_decode($authString);
+
+ /** @noinspection RegExpRedundantEscape */
+ if (preg_match("/user=(.*?)\\\1auth=Bearer (.*?)\\\1\\\1/", $decodedAuthString, $matches) !== false) {
+ $username = $matches[1];
+ $token = $matches[2];
+
+ $transport->setUsername($username);
+ $transport->setPassword($token);
+ }
+
+ $authMechanism = SmtpParams::AUTH_MECHANISM_XOAUTH;
+ } else if ($smtpParams->useAuth()) {
+ $authMechanism = $smtpParams->getAuthMechanism() ?: SmtpParams::AUTH_MECHANISM_LOGIN;
+
+ $transport->setUsername($smtpParams->getUsername() ?? '');
+ $transport->setPassword($smtpParams->getPassword() ?? '');
+ }
+
+ if ($authMechanism === SmtpParams::AUTH_MECHANISM_LOGIN) {
+ $transport->setAuthenticators([new LoginAuthenticator()]);
+ } else if ($authMechanism === SmtpParams::AUTH_MECHANISM_CRAMMD5) {
+ $transport->setAuthenticators([new CramMd5Authenticator()]);
+ } else if ($authMechanism === SmtpParams::AUTH_MECHANISM_PLAIN) {
+ $transport->setAuthenticators([new PlainAuthenticator()]);
+ } else if ($authMechanism === SmtpParams::AUTH_MECHANISM_XOAUTH) {
+ $transport->setAuthenticators([new XOAuth2Authenticator()]);
+ }
+
+ return $transport;
+ }
+}
diff --git a/application/Espo/Core/Mail/Sender/TransportPreparator.php b/application/Espo/Core/Mail/Sender/TransportPreparator.php
new file mode 100644
index 0000000000..8d5c818136
--- /dev/null
+++ b/application/Espo/Core/Mail/Sender/TransportPreparator.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;
+
+use Espo\Core\Mail\SmtpParams;
+use Symfony\Component\Mailer\Transport\TransportInterface;
+
+/**
+ * @since 9.1.0
+ */
+interface TransportPreparator
+{
+ public function prepare(SmtpParams $smtpParams): TransportInterface;
+}
diff --git a/application/Espo/Core/Mail/SmtpParams.php b/application/Espo/Core/Mail/SmtpParams.php
index b95e02ae93..749b6f63b9 100644
--- a/application/Espo/Core/Mail/SmtpParams.php
+++ b/application/Espo/Core/Mail/SmtpParams.php
@@ -50,6 +50,12 @@ class SmtpParams
private ?string $password = null;
private ?string $security = null;
+ public const AUTH_MECHANISM_LOGIN = 'login';
+ public const AUTH_MECHANISM_CRAMMD5 = 'crammd5';
+ public const AUTH_MECHANISM_PLAIN = 'plain';
+ public const AUTH_MECHANISM_XOAUTH = 'xoauth';
+
+
/** @var string[] */
private array $paramList = [
'server',
@@ -153,7 +159,7 @@ class SmtpParams
/**
* @return ?array<string, mixed>
* @deprecated As of v9.1.0.
- * @todo Remove in v10.0.
+ * @todo Remove in v10.0. Fix usage of 'connectionOptions'.
*/
public function getConnectionOptions(): ?array
{