AbstractApiTransport.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Envelope;
  12. use Symfony\Component\Mailer\Exception\RuntimeException;
  13. use Symfony\Component\Mailer\SentMessage;
  14. use Symfony\Component\Mime\Address;
  15. use Symfony\Component\Mime\Email;
  16. use Symfony\Component\Mime\MessageConverter;
  17. use Symfony\Contracts\HttpClient\ResponseInterface;
  18. /**
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. abstract class AbstractApiTransport extends AbstractHttpTransport
  22. {
  23. abstract protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface;
  24. protected function doSendHttp(SentMessage $message): ResponseInterface
  25. {
  26. try {
  27. $email = MessageConverter::toEmail($message->getOriginalMessage());
  28. } catch (\Exception $e) {
  29. throw new RuntimeException(sprintf('Unable to send message with the "%s" transport: ', __CLASS__).$e->getMessage(), 0, $e);
  30. }
  31. return $this->doSendApi($message, $email, $message->getEnvelope());
  32. }
  33. /**
  34. * @return Address[]
  35. */
  36. protected function getRecipients(Email $email, Envelope $envelope): array
  37. {
  38. return array_filter($envelope->getRecipients(), fn (Address $address) => false === \in_array($address, array_merge($email->getCc(), $email->getBcc()), true));
  39. }
  40. }