Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/sendinblue-mailer/Transport/SendinblueApiTransport.php line 37

Open in your IDE?
  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\Bridge\Sendinblue\Transport;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Mailer\Envelope;
  14. use Symfony\Component\Mailer\Exception\HttpTransportException;
  15. use Symfony\Component\Mailer\Header\MetadataHeader;
  16. use Symfony\Component\Mailer\Header\TagHeader;
  17. use Symfony\Component\Mailer\SentMessage;
  18. use Symfony\Component\Mailer\Transport\AbstractApiTransport;
  19. use Symfony\Component\Mime\Address;
  20. use Symfony\Component\Mime\Email;
  21. use Symfony\Component\Mime\Header\Headers;
  22. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  23. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  24. use Symfony\Contracts\HttpClient\HttpClientInterface;
  25. use Symfony\Contracts\HttpClient\ResponseInterface;
  26. /**
  27.  * @author Yann LUCAS
  28.  */
  29. final class SendinblueApiTransport extends AbstractApiTransport
  30. {
  31.     private $key;
  32.     public function __construct(string $keyHttpClientInterface $client nullEventDispatcherInterface $dispatcher nullLoggerInterface $logger null)
  33.     {
  34.         $this->key $key;
  35.         parent::__construct($client$dispatcher$logger);
  36.     }
  37.     public function __toString(): string
  38.     {
  39.         return sprintf('sendinblue+api://%s'$this->getEndpoint());
  40.     }
  41.     protected function doSendApi(SentMessage $sentMessageEmail $emailEnvelope $envelope): ResponseInterface
  42.     {
  43.         $response $this->client->request('POST''https://'.$this->getEndpoint().'/v3/smtp/email', [
  44.             'json' => $this->getPayload($email$envelope),
  45.             'headers' => [
  46.                 'api-key' => $this->key,
  47.             ],
  48.         ]);
  49.         try {
  50.             $statusCode $response->getStatusCode();
  51.             $result $response->toArray(false);
  52.         } catch (DecodingExceptionInterface $e) {
  53.             throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).'$statusCode), $response);
  54.         } catch (TransportExceptionInterface $e) {
  55.             throw new HttpTransportException('Could not reach the remote Sendinblue server.'$response0$e);
  56.         }
  57.         if (201 !== $statusCode) {
  58.             throw new HttpTransportException('Unable to send an email: '.($result['message'] ?? $response->getContent(false)).sprintf(' (code %d).'$statusCode), $response);
  59.         }
  60.         $sentMessage->setMessageId($result['messageId']);
  61.         return $response;
  62.     }
  63.     protected function stringifyAddresses(array $addresses): array
  64.     {
  65.         $stringifiedAddresses = [];
  66.         foreach ($addresses as $address) {
  67.             $stringifiedAddresses[] = $this->stringifyAddress($address);
  68.         }
  69.         return $stringifiedAddresses;
  70.     }
  71.     private function getPayload(Email $emailEnvelope $envelope): array
  72.     {
  73.         $payload = [
  74.             'sender' => $this->stringifyAddress($envelope->getSender()),
  75.             'to' => $this->stringifyAddresses($this->getRecipients($email$envelope)),
  76.             'subject' => $email->getSubject(),
  77.         ];
  78.         if ($attachements $this->prepareAttachments($email)) {
  79.             $payload['attachment'] = $attachements;
  80.         }
  81.         if ($emails $email->getReplyTo()) {
  82.             $payload['replyTo'] = current($this->stringifyAddresses($emails));
  83.         }
  84.         if ($emails $email->getCc()) {
  85.             $payload['cc'] = $this->stringifyAddresses($emails);
  86.         }
  87.         if ($emails $email->getBcc()) {
  88.             $payload['bcc'] = $this->stringifyAddresses($emails);
  89.         }
  90.         if ($email->getTextBody()) {
  91.             $payload['textContent'] = $email->getTextBody();
  92.         }
  93.         if ($email->getHtmlBody()) {
  94.             $payload['htmlContent'] = $email->getHtmlBody();
  95.         }
  96.         if ($headersAndTags $this->prepareHeadersAndTags($email->getHeaders())) {
  97.             $payload array_merge($payload$headersAndTags);
  98.         }
  99.         return $payload;
  100.     }
  101.     private function prepareAttachments(Email $email): array
  102.     {
  103.         $attachments = [];
  104.         foreach ($email->getAttachments() as $attachment) {
  105.             $headers $attachment->getPreparedHeaders();
  106.             $filename $headers->getHeaderParameter('Content-Disposition''filename');
  107.             $att = [
  108.                 'content' => str_replace("\r\n"''$attachment->bodyToString()),
  109.                 'name' => $filename,
  110.             ];
  111.             $attachments[] = $att;
  112.         }
  113.         return $attachments;
  114.     }
  115.     private function prepareHeadersAndTags(Headers $headers): array
  116.     {
  117.         $headersAndTags = [];
  118.         $headersToBypass = ['from''sender''to''cc''bcc''subject''reply-to''content-type''accept''api-key'];
  119.         foreach ($headers->all() as $name => $header) {
  120.             if (\in_array($name$headersToBypasstrue)) {
  121.                 continue;
  122.             }
  123.             if ($header instanceof TagHeader) {
  124.                 $headersAndTags['tags'][] = $header->getValue();
  125.                 continue;
  126.             }
  127.             if ($header instanceof MetadataHeader) {
  128.                 $headersAndTags['headers']['X-Mailin-'.ucfirst(strtolower($header->getKey()))] = $header->getValue();
  129.                 continue;
  130.             }
  131.             if ('templateid' === $name) {
  132.                 $headersAndTags[$header->getName()] = (int) $header->getValue();
  133.                 continue;
  134.             }
  135.             if ('params' === $name) {
  136.                 $headersAndTags[$header->getName()] = $header->getParameters();
  137.                 continue;
  138.             }
  139.             $headersAndTags['headers'][$header->getName()] = $header->getBodyAsString();
  140.         }
  141.         return $headersAndTags;
  142.     }
  143.     private function stringifyAddress(Address $address): array
  144.     {
  145.         $stringifiedAddress = ['email' => $address->getAddress()];
  146.         if ($address->getName()) {
  147.             $stringifiedAddress['name'] = $address->getName();
  148.         }
  149.         return $stringifiedAddress;
  150.     }
  151.     private function getEndpoint(): ?string
  152.     {
  153.         return ($this->host ?: 'api.brevo.com').($this->port ':'.$this->port '');
  154.     }
  155. }