AbstractApiTransport.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. protected function getRecipients(Email $email, Envelope $envelope): array
  34. {
  35. return array_filter($envelope->getRecipients(), function (Address $address) use ($email) {
  36. return false === \in_array($address, array_merge($email->getCc(), $email->getBcc()), true);
  37. });
  38. }
  39. }