EsmtpTransportFactory.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Transport\Smtp;
  11. use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
  12. use Symfony\Component\Mailer\Transport\Dsn;
  13. use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
  14. use Symfony\Component\Mailer\Transport\TransportInterface;
  15. /**
  16. * @author Konstantin Myakshin <molodchick@gmail.com>
  17. */
  18. final class EsmtpTransportFactory extends AbstractTransportFactory
  19. {
  20. public function create(Dsn $dsn): TransportInterface
  21. {
  22. $tls = 'smtps' === $dsn->getScheme() ? true : null;
  23. $port = $dsn->getPort(0);
  24. $host = $dsn->getHost();
  25. $transport = new EsmtpTransport($host, $port, $tls, $this->dispatcher, $this->logger);
  26. if ('' !== $dsn->getOption('verify_peer') && !filter_var($dsn->getOption('verify_peer', true), \FILTER_VALIDATE_BOOLEAN)) {
  27. /** @var SocketStream $stream */
  28. $stream = $transport->getStream();
  29. $streamOptions = $stream->getStreamOptions();
  30. $streamOptions['ssl']['verify_peer'] = false;
  31. $streamOptions['ssl']['verify_peer_name'] = false;
  32. $stream->setStreamOptions($streamOptions);
  33. }
  34. if ($user = $dsn->getUser()) {
  35. $transport->setUsername($user);
  36. }
  37. if ($password = $dsn->getPassword()) {
  38. $transport->setPassword($password);
  39. }
  40. if (null !== ($localDomain = $dsn->getOption('local_domain'))) {
  41. $transport->setLocalDomain($localDomain);
  42. }
  43. if (null !== ($restartThreshold = $dsn->getOption('restart_threshold'))) {
  44. $transport->setRestartThreshold((int) $restartThreshold, (int) $dsn->getOption('restart_threshold_sleep', 0));
  45. }
  46. if (null !== ($pingThreshold = $dsn->getOption('ping_threshold'))) {
  47. $transport->setPingThreshold((int) $pingThreshold);
  48. }
  49. return $transport;
  50. }
  51. protected function getSupportedSchemes(): array
  52. {
  53. return ['smtp', 'smtps'];
  54. }
  55. }