NativeTransportFactory.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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;
  11. use Symfony\Component\Mailer\Exception\TransportException;
  12. use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
  13. use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
  14. use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
  15. /**
  16. * Factory that configures a transport (sendmail or SMTP) based on php.ini settings.
  17. *
  18. * @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
  19. */
  20. final class NativeTransportFactory extends AbstractTransportFactory
  21. {
  22. public function create(Dsn $dsn): TransportInterface
  23. {
  24. if (!\in_array($dsn->getScheme(), $this->getSupportedSchemes(), true)) {
  25. throw new UnsupportedSchemeException($dsn, 'native', $this->getSupportedSchemes());
  26. }
  27. if ($sendMailPath = ini_get('sendmail_path')) {
  28. return new SendmailTransport($sendMailPath, $this->dispatcher, $this->logger);
  29. }
  30. if ('\\' !== \DIRECTORY_SEPARATOR) {
  31. throw new TransportException('sendmail_path is not configured in php.ini.');
  32. }
  33. // Only for windows hosts; at this point non-windows
  34. // host have already thrown an exception or returned a transport
  35. $host = ini_get('smtp');
  36. $port = (int) ini_get('smtp_port');
  37. if (!$host || !$port) {
  38. throw new TransportException('smtp or smtp_port is not configured in php.ini.');
  39. }
  40. $socketStream = new SocketStream();
  41. $socketStream->setHost($host);
  42. $socketStream->setPort($port);
  43. if (465 !== $port) {
  44. $socketStream->disableTls();
  45. }
  46. return new SmtpTransport($socketStream, $this->dispatcher, $this->logger);
  47. }
  48. protected function getSupportedSchemes(): array
  49. {
  50. return ['native'];
  51. }
  52. }