Transport.php 7.6 KB

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