Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. EsmtpTransport.php

class EsmtpTransport

Sends Emails over SMTP with ESMTP support.

@author Fabien Potencier <fabien@symfony.com> @author Chris Corbyn

Hierarchy

  • class \Symfony\Component\Mailer\Transport\AbstractTransport implements \Symfony\Component\Mailer\Transport\TransportInterface
    • class \Symfony\Component\Mailer\Transport\Smtp\SmtpTransport extends \Symfony\Component\Mailer\Transport\AbstractTransport
      • class \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport extends \Symfony\Component\Mailer\Transport\Smtp\SmtpTransport

Expanded class hierarchy of EsmtpTransport

5 files declare their use of EsmtpTransport
AuthenticatorInterface.php in vendor/symfony/mailer/Transport/Smtp/Auth/AuthenticatorInterface.php
CramMd5Authenticator.php in vendor/symfony/mailer/Transport/Smtp/Auth/CramMd5Authenticator.php
LoginAuthenticator.php in vendor/symfony/mailer/Transport/Smtp/Auth/LoginAuthenticator.php
PlainAuthenticator.php in vendor/symfony/mailer/Transport/Smtp/Auth/PlainAuthenticator.php
XOAuth2Authenticator.php in vendor/symfony/mailer/Transport/Smtp/Auth/XOAuth2Authenticator.php

File

vendor/symfony/mailer/Transport/Smtp/EsmtpTransport.php, line 29

Namespace

Symfony\Component\Mailer\Transport\Smtp
View source
class EsmtpTransport extends SmtpTransport {
    private array $authenticators = [];
    private string $username = '';
    private string $password = '';
    private array $capabilities;
    private bool $autoTls = true;
    public function __construct(string $host = 'localhost', int $port = 0, ?bool $tls = null, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, ?AbstractStream $stream = null, ?array $authenticators = null) {
        parent::__construct($stream, $dispatcher, $logger);
        if (null === $authenticators) {
            // fallback to default authenticators
            // order is important here (roughly most secure and popular first)
            $authenticators = [
                new Auth\CramMd5Authenticator(),
                new Auth\LoginAuthenticator(),
                new Auth\PlainAuthenticator(),
                new Auth\XOAuth2Authenticator(),
            ];
        }
        $this->setAuthenticators($authenticators);
        
        /** @var SocketStream $stream */
        $stream = $this->getStream();
        if (null === $tls) {
            if (465 === $port) {
                $tls = true;
            }
            else {
                $tls = \defined('OPENSSL_VERSION_NUMBER') && 0 === $port && 'localhost' !== $host;
            }
        }
        if (!$tls) {
            $stream->disableTls();
        }
        if (0 === $port) {
            $port = $tls ? 465 : 25;
        }
        $stream->setHost($host);
        $stream->setPort($port);
    }
    
    /**
     * @return $this
     */
    public function setUsername(string $username) : static {
        $this->username = $username;
        return $this;
    }
    public function getUsername() : string {
        return $this->username;
    }
    
    /**
     * @return $this
     */
    public function setPassword(string $password) : static {
        $this->password = $password;
        return $this;
    }
    public function getPassword() : string {
        return $this->password;
    }
    
    /**
     * @return $this
     */
    public function setAutoTls(bool $autoTls) : static {
        $this->autoTls = $autoTls;
        return $this;
    }
    public function isAutoTls() : bool {
        return $this->autoTls;
    }
    public function setAuthenticators(array $authenticators) : void {
        $this->authenticators = [];
        foreach ($authenticators as $authenticator) {
            $this->addAuthenticator($authenticator);
        }
    }
    public function addAuthenticator(AuthenticatorInterface $authenticator) : void {
        $this->authenticators[] = $authenticator;
    }
    public function executeCommand(string $command, array $codes) : string {
        return [
            250,
        ] === $codes && str_starts_with($command, 'HELO ') ? $this->doEhloCommand() : parent::executeCommand($command, $codes);
    }
    protected final function getCapabilities() : array {
        return $this->capabilities;
    }
    private function doEhloCommand() : string {
        try {
            $response = $this->executeCommand(\sprintf("EHLO %s\r\n", $this->getLocalDomain()), [
                250,
            ]);
        } catch (TransportExceptionInterface $e) {
            try {
                return parent::executeCommand(\sprintf("HELO %s\r\n", $this->getLocalDomain()), [
                    250,
                ]);
            } catch (TransportExceptionInterface $ex) {
                if (!$ex->getCode()) {
                    throw $e;
                }
                throw $ex;
            }
        }
        $this->capabilities = $this->parseCapabilities($response);
        
        /** @var SocketStream $stream */
        $stream = $this->getStream();
        // WARNING: !$stream->isTLS() is right, 100% sure :)
        // if you think that the ! should be removed, read the code again
        // if doing so "fixes" your issue then it probably means your SMTP server behaves incorrectly or is wrongly configured
        if ($this->autoTls && !$stream->isTLS() && \defined('OPENSSL_VERSION_NUMBER') && \array_key_exists('STARTTLS', $this->capabilities)) {
            $this->executeCommand("STARTTLS\r\n", [
                220,
            ]);
            if (!$stream->startTLS()) {
                throw new TransportException('Unable to connect with STARTTLS.');
            }
            $response = $this->executeCommand(\sprintf("EHLO %s\r\n", $this->getLocalDomain()), [
                250,
            ]);
            $this->capabilities = $this->parseCapabilities($response);
        }
        if (\array_key_exists('AUTH', $this->capabilities)) {
            $this->handleAuth($this->capabilities['AUTH']);
        }
        return $response;
    }
    private function parseCapabilities(string $ehloResponse) : array {
        $capabilities = [];
        $lines = explode("\r\n", trim($ehloResponse));
        array_shift($lines);
        foreach ($lines as $line) {
            if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di', $line, $matches)) {
                $value = strtoupper(ltrim($matches[2], ' ='));
                $capabilities[strtoupper($matches[1])] = $value ? explode(' ', $value) : [];
            }
        }
        return $capabilities;
    }
    protected function serverSupportsSmtpUtf8() : bool {
        return \array_key_exists('SMTPUTF8', $this->capabilities);
    }
    private function handleAuth(array $modes) : void {
        if (!$this->username) {
            return;
        }
        $code = null;
        $authNames = [];
        $errors = [];
        $modes = array_map('strtolower', $modes);
        foreach ($this->authenticators as $authenticator) {
            if (!\in_array(strtolower($authenticator->getAuthKeyword()), $modes, true)) {
                continue;
            }
            $code = null;
            $authNames[] = $authenticator->getAuthKeyword();
            try {
                $authenticator->authenticate($this);
                return;
            } catch (UnexpectedResponseException $e) {
                $code = $e->getCode();
                try {
                    $this->executeCommand("RSET\r\n", [
                        250,
                    ]);
                } catch (TransportExceptionInterface) {
                    // ignore this exception as it probably means that the server error was final
                }
                // keep the error message, but tries the other authenticators
                $errors[$authenticator->getAuthKeyword()] = $e->getMessage();
            }
        }
        if (!$authNames) {
            throw new TransportException(\sprintf('Failed to find an authenticator supported by the SMTP server, which currently supports: "%s".', implode('", "', $modes)), $code ?: 504);
        }
        $message = \sprintf('Failed to authenticate on SMTP server with username "%s" using the following authenticators: "%s".', $this->username, implode('", "', $authNames));
        foreach ($errors as $name => $error) {
            $message .= \sprintf(' Authenticator "%s" returned "%s".', $name, $error);
        }
        throw new TransportException($message, $code ?: 535);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractTransport::$lastSent private property
AbstractTransport::$logger private property
AbstractTransport::$rate private property
AbstractTransport::checkThrottling private function
AbstractTransport::getLogger protected function
AbstractTransport::setMaxPerSecond public function Sets the maximum number of messages to send per second (0 to disable).
AbstractTransport::stringifyAddresses protected function
EsmtpTransport::$authenticators private property
EsmtpTransport::$autoTls private property
EsmtpTransport::$capabilities private property
EsmtpTransport::$password private property
EsmtpTransport::$username private property
EsmtpTransport::addAuthenticator public function
EsmtpTransport::doEhloCommand private function
EsmtpTransport::executeCommand public function Runs a command against the stream, expecting the given response codes. Overrides SmtpTransport::executeCommand
EsmtpTransport::getCapabilities final protected function
EsmtpTransport::getPassword public function
EsmtpTransport::getUsername public function
EsmtpTransport::handleAuth private function
EsmtpTransport::isAutoTls public function
EsmtpTransport::parseCapabilities private function
EsmtpTransport::serverSupportsSmtpUtf8 protected function Overrides SmtpTransport::serverSupportsSmtpUtf8
EsmtpTransport::setAuthenticators public function
EsmtpTransport::setAutoTls public function
EsmtpTransport::setPassword public function
EsmtpTransport::setUsername public function
EsmtpTransport::__construct public function Overrides SmtpTransport::__construct
SmtpTransport::$domain private property
SmtpTransport::$lastMessageTime private property
SmtpTransport::$pingThreshold private property
SmtpTransport::$restartCounter private property
SmtpTransport::$restartThreshold private property
SmtpTransport::$restartThresholdSleep private property
SmtpTransport::$started private property
SmtpTransport::$stream private property
SmtpTransport::assertResponseCode private function
SmtpTransport::checkRestartThreshold private function
SmtpTransport::doHeloCommand private function
SmtpTransport::doMailFromCommand private function
SmtpTransport::doRcptToCommand private function
SmtpTransport::doSend protected function Overrides AbstractTransport::doSend
SmtpTransport::getFullResponse private function
SmtpTransport::getLocalDomain public function Gets the name of the domain that will be used in HELO.
SmtpTransport::getStream public function
SmtpTransport::parseMessageId protected function
SmtpTransport::ping private function
SmtpTransport::send public function Overrides AbstractTransport::send
SmtpTransport::setLocalDomain public function Sets the name of the local domain that will be used in HELO.
SmtpTransport::setPingThreshold public function Sets the minimum number of seconds required between two messages, before the server is pinged.
If the transport wants to send a message and the time since the last message exceeds the specified threshold,
the transport will ping the server first (NOOP…
SmtpTransport::setRestartThreshold public function Sets the maximum number of messages to send before re-starting the transport.
SmtpTransport::start public function
SmtpTransport::stop public function Manually disconnect from the SMTP server.
SmtpTransport::__destruct public function
SmtpTransport::__sleep public function
SmtpTransport::__toString public function
SmtpTransport::__wakeup public function
RSS feed
Powered by Drupal