vendor/symfony/swiftmailer-bundle/DependencyInjection/SwiftmailerTransportFactory.php line 28

Open in your IDE?
  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\Bundle\SwiftmailerBundle\DependencyInjection;
  11. use Symfony\Component\Routing\RequestContext;
  12. /**
  13.  * Factory to create a \Swift_Transport object.
  14.  *
  15.  * @author Romain Gautier <mail@romain.sh>
  16.  */
  17. class SwiftmailerTransportFactory
  18. {
  19.     /**
  20.      * @return \Swift_Transport
  21.      *
  22.      * @throws \InvalidArgumentException if the scheme is not a built-in Swiftmailer transport
  23.      */
  24.     public static function createTransport(array $optionsRequestContext $requestContext null\Swift_Events_EventDispatcher $eventDispatcher)
  25.     {
  26.         $options = static::resolveOptions($options);
  27.         self::validateConfig($options);
  28.         if ('smtp' === $options['transport']) {
  29.             $smtpAuthHandler = new \Swift_Transport_Esmtp_AuthHandler([
  30.                 new \Swift_Transport_Esmtp_Auth_CramMd5Authenticator(),
  31.                 new \Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
  32.                 new \Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
  33.                 new \Swift_Transport_Esmtp_Auth_NTLMAuthenticator(),
  34.             ]);
  35.             $smtpAuthHandler->setUsername($options['username']);
  36.             $smtpAuthHandler->setPassword($options['password']);
  37.             $smtpAuthHandler->setAuthMode($options['auth_mode']);
  38.             $transport = new \Swift_Transport_EsmtpTransport(
  39.                 new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory()),
  40.                 [$smtpAuthHandler],
  41.                 $eventDispatcher
  42.             );
  43.             $transport->setHost($options['host']);
  44.             $transport->setPort($options['port']);
  45.             $transport->setEncryption($options['encryption']);
  46.             $transport->setTimeout($options['timeout']);
  47.             $transport->setSourceIp($options['source_ip']);
  48.             $transport->setStreamOptions($options['stream_options']);
  49.             $smtpTransportConfigurator = new SmtpTransportConfigurator($options['local_domain'], $requestContext);
  50.             $smtpTransportConfigurator->configure($transport);
  51.         } elseif ('sendmail' === $options['transport']) {
  52.             $transport = new \Swift_Transport_SendmailTransport(
  53.                 new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory()),
  54.                 $eventDispatcher
  55.             );
  56.             $transport->setCommand($options['command']);
  57.             $smtpTransportConfigurator = new SmtpTransportConfigurator($options['local_domain'], $requestContext);
  58.             $smtpTransportConfigurator->configure($transport);
  59.         } elseif ('null' === $options['transport']) {
  60.             $transport = new \Swift_Transport_NullTransport($eventDispatcher);
  61.         } else {
  62.             throw new \InvalidArgumentException(sprintf('Not a built-in Swiftmailer transport: %s.'$options['transport']));
  63.         }
  64.         return $transport;
  65.     }
  66.     /**
  67.      * @return array options
  68.      */
  69.     public static function resolveOptions(array $options)
  70.     {
  71.         $options += [
  72.             'transport' => null,
  73.             'username' => null,
  74.             'password' => null,
  75.             'host' => null,
  76.             'port' => null,
  77.             'timeout' => null,
  78.             'source_ip' => null,
  79.             'local_domain' => null,
  80.             'encryption' => null,
  81.             'auth_mode' => null,
  82.             'command' => null,
  83.             'stream_options' => [],
  84.         ];
  85.         if (isset($options['url'])) {
  86.             if (false === $parts parse_url($options['url'])) {
  87.                 throw new \InvalidArgumentException(sprintf('The Swiftmailer URL "%s" is not valid.'$options['url']));
  88.             }
  89.             if (isset($parts['scheme'])) {
  90.                 $options['transport'] = $parts['scheme'];
  91.             }
  92.             if (isset($parts['user'])) {
  93.                 $options['username'] = rawurldecode($parts['user']);
  94.             }
  95.             if (isset($parts['pass'])) {
  96.                 $options['password'] = rawurldecode($parts['pass']);
  97.             }
  98.             if (isset($parts['host'])) {
  99.                 $options['host'] = rawurldecode($parts['host']);
  100.             }
  101.             if (isset($parts['port'])) {
  102.                 $options['port'] = $parts['port'];
  103.             }
  104.             if (isset($parts['query'])) {
  105.                 parse_str($parts['query'], $query);
  106.                 foreach ($options as $key => $value) {
  107.                     if (isset($query[$key]) && '' != $query[$key]) {
  108.                         $options[$key] = $query[$key];
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.         if (!isset($options['transport'])) {
  114.             $options['transport'] = 'null';
  115.         } elseif ('gmail' === $options['transport']) {
  116.             $options['encryption'] = 'ssl';
  117.             $options['auth_mode'] = 'login';
  118.             $options['host'] = 'smtp.gmail.com';
  119.             $options['transport'] = 'smtp';
  120.         }
  121.         if (!isset($options['port'])) {
  122.             $options['port'] = 'ssl' === $options['encryption'] ? 465 25;
  123.         }
  124.         return $options;
  125.     }
  126.     /**
  127.      * @throws \InvalidArgumentException if the encryption is not valid
  128.      */
  129.     public static function validateConfig($options)
  130.     {
  131.         if (!\in_array($options['encryption'], ['tcp''tls''ssl'null], true)) {
  132.             throw new \InvalidArgumentException(sprintf('The %s encryption is not supported'$options['encryption']));
  133.         }
  134.         if (!\in_array($options['auth_mode'], ['plain''login''cram-md5''ntlm'null], true)) {
  135.             throw new \InvalidArgumentException(sprintf('The %s authentication mode is not supported'$options['auth_mode']));
  136.         }
  137.     }
  138. }