EsmtpTransportFactory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. /** @var SocketStream $stream */
  27. $stream = $transport->getStream();
  28. $streamOptions = $stream->getStreamOptions();
  29. if ('' !== $dsn->getOption('verify_peer') && !filter_var($dsn->getOption('verify_peer', true), \FILTER_VALIDATE_BOOL)) {
  30. $streamOptions['ssl']['verify_peer'] = false;
  31. $streamOptions['ssl']['verify_peer_name'] = false;
  32. }
  33. if (null !== $peerFingerprint = $dsn->getOption('peer_fingerprint')) {
  34. $streamOptions['ssl']['peer_fingerprint'] = $peerFingerprint;
  35. }
  36. $stream->setStreamOptions($streamOptions);
  37. if ($user = $dsn->getUser()) {
  38. $transport->setUsername($user);
  39. }
  40. if ($password = $dsn->getPassword()) {
  41. $transport->setPassword($password);
  42. }
  43. if (null !== ($localDomain = $dsn->getOption('local_domain'))) {
  44. $transport->setLocalDomain($localDomain);
  45. }
  46. if (null !== ($maxPerSecond = $dsn->getOption('max_per_second'))) {
  47. $transport->setMaxPerSecond((float) $maxPerSecond);
  48. }
  49. if (null !== ($restartThreshold = $dsn->getOption('restart_threshold'))) {
  50. $transport->setRestartThreshold((int) $restartThreshold, (int) $dsn->getOption('restart_threshold_sleep', 0));
  51. }
  52. if (null !== ($pingThreshold = $dsn->getOption('ping_threshold'))) {
  53. $transport->setPingThreshold((int) $pingThreshold);
  54. }
  55. return $transport;
  56. }
  57. protected function getSupportedSchemes(): array
  58. {
  59. return ['smtp', 'smtps'];
  60. }
  61. }