Transport.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Azure\Transport\AzureTransportFactory;
  15. use Symfony\Component\Mailer\Bridge\Brevo\Transport\BrevoTransportFactory;
  16. use Symfony\Component\Mailer\Bridge\Google\Transport\GmailTransportFactory;
  17. use Symfony\Component\Mailer\Bridge\Infobip\Transport\InfobipTransportFactory;
  18. use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
  19. use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendTransportFactory;
  20. use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
  21. use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
  22. use Symfony\Component\Mailer\Bridge\MailPace\Transport\MailPaceTransportFactory;
  23. use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
  24. use Symfony\Component\Mailer\Bridge\Resend\Transport\ResendTransportFactory;
  25. use Symfony\Component\Mailer\Bridge\Scaleway\Transport\ScalewayTransportFactory;
  26. use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
  27. use Symfony\Component\Mailer\Exception\InvalidArgumentException;
  28. use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
  29. use Symfony\Component\Mailer\Transport\Dsn;
  30. use Symfony\Component\Mailer\Transport\FailoverTransport;
  31. use Symfony\Component\Mailer\Transport\NativeTransportFactory;
  32. use Symfony\Component\Mailer\Transport\NullTransportFactory;
  33. use Symfony\Component\Mailer\Transport\RoundRobinTransport;
  34. use Symfony\Component\Mailer\Transport\SendmailTransportFactory;
  35. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
  36. use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
  37. use Symfony\Component\Mailer\Transport\TransportInterface;
  38. use Symfony\Component\Mailer\Transport\Transports;
  39. use Symfony\Contracts\HttpClient\HttpClientInterface;
  40. /**
  41. * @author Fabien Potencier <fabien@symfony.com>
  42. * @author Konstantin Myakshin <molodchick@gmail.com>
  43. */
  44. final class Transport
  45. {
  46. private const FACTORY_CLASSES = [
  47. AzureTransportFactory::class,
  48. BrevoTransportFactory::class,
  49. GmailTransportFactory::class,
  50. InfobipTransportFactory::class,
  51. MailerSendTransportFactory::class,
  52. MailgunTransportFactory::class,
  53. MailjetTransportFactory::class,
  54. MailPaceTransportFactory::class,
  55. MandrillTransportFactory::class,
  56. PostmarkTransportFactory::class,
  57. ResendTransportFactory::class,
  58. ScalewayTransportFactory::class,
  59. SendgridTransportFactory::class,
  60. SesTransportFactory::class,
  61. ];
  62. private iterable $factories;
  63. public static function fromDsn(#[\SensitiveParameter] string $dsn, ?EventDispatcherInterface $dispatcher = null, ?HttpClientInterface $client = null, ?LoggerInterface $logger = null): TransportInterface
  64. {
  65. $factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher, $client, $logger)));
  66. return $factory->fromString($dsn);
  67. }
  68. public static function fromDsns(#[\SensitiveParameter] array $dsns, ?EventDispatcherInterface $dispatcher = null, ?HttpClientInterface $client = null, ?LoggerInterface $logger = null): TransportInterface
  69. {
  70. $factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher, $client, $logger)));
  71. return $factory->fromStrings($dsns);
  72. }
  73. /**
  74. * @param TransportFactoryInterface[] $factories
  75. */
  76. public function __construct(iterable $factories)
  77. {
  78. $this->factories = $factories;
  79. }
  80. public function fromStrings(#[\SensitiveParameter] array $dsns): Transports
  81. {
  82. $transports = [];
  83. foreach ($dsns as $name => $dsn) {
  84. $transports[$name] = $this->fromString($dsn);
  85. }
  86. return new Transports($transports);
  87. }
  88. public function fromString(#[\SensitiveParameter] string $dsn): TransportInterface
  89. {
  90. [$transport, $offset] = $this->parseDsn($dsn);
  91. if ($offset !== \strlen($dsn)) {
  92. throw new InvalidArgumentException('The mailer DSN has some garbage at the end.');
  93. }
  94. return $transport;
  95. }
  96. private function parseDsn(#[\SensitiveParameter] string $dsn, int $offset = 0): array
  97. {
  98. static $keywords = [
  99. 'failover' => FailoverTransport::class,
  100. 'roundrobin' => RoundRobinTransport::class,
  101. ];
  102. while (true) {
  103. foreach ($keywords as $name => $class) {
  104. $name .= '(';
  105. if ($name === substr($dsn, $offset, \strlen($name))) {
  106. $offset += \strlen($name) - 1;
  107. preg_match('{\(([^()]|(?R))*\)}A', $dsn, $matches, 0, $offset);
  108. if (!isset($matches[0])) {
  109. continue;
  110. }
  111. ++$offset;
  112. $args = [];
  113. while (true) {
  114. [$arg, $offset] = $this->parseDsn($dsn, $offset);
  115. $args[] = $arg;
  116. if (\strlen($dsn) === $offset) {
  117. break;
  118. }
  119. ++$offset;
  120. if (')' === $dsn[$offset - 1]) {
  121. break;
  122. }
  123. }
  124. return [new $class($args), $offset];
  125. }
  126. }
  127. if (preg_match('{(\w+)\(}A', $dsn, $matches, 0, $offset)) {
  128. throw new InvalidArgumentException(sprintf('The "%s" keyword is not valid (valid ones are "%s"), ', $matches[1], implode('", "', array_keys($keywords))));
  129. }
  130. if ($pos = strcspn($dsn, ' )', $offset)) {
  131. return [$this->fromDsnObject(Dsn::fromString(substr($dsn, $offset, $pos))), $offset + $pos];
  132. }
  133. return [$this->fromDsnObject(Dsn::fromString(substr($dsn, $offset))), \strlen($dsn)];
  134. }
  135. }
  136. public function fromDsnObject(Dsn $dsn): TransportInterface
  137. {
  138. foreach ($this->factories as $factory) {
  139. if ($factory->supports($dsn)) {
  140. return $factory->create($dsn);
  141. }
  142. }
  143. throw new UnsupportedSchemeException($dsn);
  144. }
  145. /**
  146. * @return \Traversable<int, TransportFactoryInterface>
  147. */
  148. public static function getDefaultFactories(?EventDispatcherInterface $dispatcher = null, ?HttpClientInterface $client = null, ?LoggerInterface $logger = null): \Traversable
  149. {
  150. foreach (self::FACTORY_CLASSES as $factoryClass) {
  151. if (class_exists($factoryClass)) {
  152. yield new $factoryClass($dispatcher, $client, $logger);
  153. }
  154. }
  155. yield new NullTransportFactory($dispatcher, $client, $logger);
  156. yield new SendmailTransportFactory($dispatcher, $client, $logger);
  157. yield new EsmtpTransportFactory($dispatcher, $client, $logger);
  158. yield new NativeTransportFactory($dispatcher, $client, $logger);
  159. }
  160. }