vendor/symfony/mailer/Transport.php line 143

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Mailer;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesTransportFactory;
  14. use Symfony\Component\Mailer\Bridge\Google\Transport\GmailTransportFactory;
  15. use Symfony\Component\Mailer\Bridge\Infobip\Transport\InfobipTransportFactory;
  16. use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
  17. use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
  18. use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
  19. use Symfony\Component\Mailer\Bridge\OhMySmtp\Transport\OhMySmtpTransportFactory;
  20. use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
  21. use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
  22. use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
  23. use Symfony\Component\Mailer\Exception\InvalidArgumentException;
  24. use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
  25. use Symfony\Component\Mailer\Transport\Dsn;
  26. use Symfony\Component\Mailer\Transport\FailoverTransport;
  27. use Symfony\Component\Mailer\Transport\NativeTransportFactory;
  28. use Symfony\Component\Mailer\Transport\NullTransportFactory;
  29. use Symfony\Component\Mailer\Transport\RoundRobinTransport;
  30. use Symfony\Component\Mailer\Transport\SendmailTransportFactory;
  31. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
  32. use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
  33. use Symfony\Component\Mailer\Transport\TransportInterface;
  34. use Symfony\Component\Mailer\Transport\Transports;
  35. use Symfony\Contracts\HttpClient\HttpClientInterface;
  36. /**
  37.  * @author Fabien Potencier <fabien@symfony.com>
  38.  * @author Konstantin Myakshin <molodchick@gmail.com>
  39.  */
  40. final class Transport
  41. {
  42.     private const FACTORY_CLASSES = [
  43.         GmailTransportFactory::class,
  44.         InfobipTransportFactory::class,
  45.         MailgunTransportFactory::class,
  46.         MailjetTransportFactory::class,
  47.         MandrillTransportFactory::class,
  48.         OhMySmtpTransportFactory::class,
  49.         PostmarkTransportFactory::class,
  50.         SendgridTransportFactory::class,
  51.         SendinblueTransportFactory::class,
  52.         SesTransportFactory::class,
  53.     ];
  54.     private iterable $factories;
  55.     public static function fromDsn(string $dsnEventDispatcherInterface $dispatcher nullHttpClientInterface $client nullLoggerInterface $logger null): TransportInterface
  56.     {
  57.         $factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher$client$logger)));
  58.         return $factory->fromString($dsn);
  59.     }
  60.     public static function fromDsns(array $dsnsEventDispatcherInterface $dispatcher nullHttpClientInterface $client nullLoggerInterface $logger null): TransportInterface
  61.     {
  62.         $factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher$client$logger)));
  63.         return $factory->fromStrings($dsns);
  64.     }
  65.     /**
  66.      * @param TransportFactoryInterface[] $factories
  67.      */
  68.     public function __construct(iterable $factories)
  69.     {
  70.         $this->factories $factories;
  71.     }
  72.     public function fromStrings(array $dsns): Transports
  73.     {
  74.         $transports = [];
  75.         foreach ($dsns as $name => $dsn) {
  76.             $transports[$name] = $this->fromString($dsn);
  77.         }
  78.         return new Transports($transports);
  79.     }
  80.     public function fromString(string $dsn): TransportInterface
  81.     {
  82.         [$transport$offset] = $this->parseDsn($dsn);
  83.         if ($offset !== \strlen($dsn)) {
  84.             throw new InvalidArgumentException(sprintf('The DSN has some garbage at the end: "%s".'substr($dsn$offset)));
  85.         }
  86.         return $transport;
  87.     }
  88.     private function parseDsn(string $dsnint $offset 0): array
  89.     {
  90.         static $keywords = [
  91.             'failover' => FailoverTransport::class,
  92.             'roundrobin' => RoundRobinTransport::class,
  93.         ];
  94.         while (true) {
  95.             foreach ($keywords as $name => $class) {
  96.                 $name .= '(';
  97.                 if ($name === substr($dsn$offset\strlen($name))) {
  98.                     $offset += \strlen($name) - 1;
  99.                     preg_match('{\(([^()]|(?R))*\)}A'$dsn$matches0$offset);
  100.                     if (!isset($matches[0])) {
  101.                         continue;
  102.                     }
  103.                     ++$offset;
  104.                     $args = [];
  105.                     while (true) {
  106.                         [$arg$offset] = $this->parseDsn($dsn$offset);
  107.                         $args[] = $arg;
  108.                         if (\strlen($dsn) === $offset) {
  109.                             break;
  110.                         }
  111.                         ++$offset;
  112.                         if (')' === $dsn[$offset 1]) {
  113.                             break;
  114.                         }
  115.                     }
  116.                     return [new $class($args), $offset];
  117.                 }
  118.             }
  119.             if (preg_match('{(\w+)\(}A'$dsn$matches0$offset)) {
  120.                 throw new InvalidArgumentException(sprintf('The "%s" keyword is not valid (valid ones are "%s"), '$matches[1], implode('", "'array_keys($keywords))));
  121.             }
  122.             if ($pos strcspn($dsn' )'$offset)) {
  123.                 return [$this->fromDsnObject(Dsn::fromString(substr($dsn$offset$pos))), $offset $pos];
  124.             }
  125.             return [$this->fromDsnObject(Dsn::fromString(substr($dsn$offset))), \strlen($dsn)];
  126.         }
  127.     }
  128.     public function fromDsnObject(Dsn $dsn): TransportInterface
  129.     {
  130.         foreach ($this->factories as $factory) {
  131.             if ($factory->supports($dsn)) {
  132.                 return $factory->create($dsn);
  133.             }
  134.         }
  135.         throw new UnsupportedSchemeException($dsn);
  136.     }
  137.     /**
  138.      * @return \Traversable<int, TransportFactoryInterface>
  139.      */
  140.     public static function getDefaultFactories(EventDispatcherInterface $dispatcher nullHttpClientInterface $client nullLoggerInterface $logger null): \Traversable
  141.     {
  142.         foreach (self::FACTORY_CLASSES as $factoryClass) {
  143.             if (class_exists($factoryClass)) {
  144.                 yield new $factoryClass($dispatcher$client$logger);
  145.             }
  146.         }
  147.         yield new NullTransportFactory($dispatcher$client$logger);
  148.         yield new SendmailTransportFactory($dispatcher$client$logger);
  149.         yield new EsmtpTransportFactory($dispatcher$client$logger);
  150.         yield new NativeTransportFactory($dispatcher$client$logger);
  151.     }
  152. }