From 38a7ee534a8f957c54ff28bcb16a148219c439be Mon Sep 17 00:00:00 2001 From: Florian Heller Date: Wed, 31 May 2023 17:37:20 +0200 Subject: [PATCH] [Mailer] [MailPace] Fix undefined array key in errors response --- .../Transport/MailPaceApiTransportTest.php | 51 +++++++++++++++++++ .../Transport/MailPaceApiTransport.php | 15 +++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Mailer/Bridge/MailPace/Tests/Transport/MailPaceApiTransportTest.php b/src/Symfony/Component/Mailer/Bridge/MailPace/Tests/Transport/MailPaceApiTransportTest.php index 065c071e52856..c7e99b46d02d4 100644 --- a/src/Symfony/Component/Mailer/Bridge/MailPace/Tests/Transport/MailPaceApiTransportTest.php +++ b/src/Symfony/Component/Mailer/Bridge/MailPace/Tests/Transport/MailPaceApiTransportTest.php @@ -121,6 +121,57 @@ public function testSendThrowsForErrorResponse() $transport->send($mail); } + public function testSendThrowsForErrorsResponse() + { + $client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface { + return new MockResponse(json_encode([ + 'errors' => [ + 'to' => [ + 'contains a blocked address', + 'number of email addresses exceeds maximum volume', + ], + 'attachments.name' => ['Extension file type blocked, see Docs for full list of allowed file types'], + ], + ]), [ + 'http_code' => 400, + 'response_headers' => [ + 'content-type' => 'application/json', + ], + ]); + }); + $transport = new MailPaceApiTransport('KEY', $client); + $transport->setPort(8984); + + $mail = new Email(); + $mail->subject('Hello!') + ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin')) + ->from(new Address('fabpot@symfony.com', 'Fabien')) + ->text('Hello There!'); + + $this->expectException(HttpTransportException::class); + $this->expectExceptionMessage('Unable to send an email: to: contains a blocked address & number of email addresses exceeds maximum volume; attachments.name: Extension file type blocked, see Docs for full list of allowed file types (code 400).'); + $transport->send($mail); + } + + public function testSendThrowsForInternalServerErrorResponse() + { + $client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface { + return new MockResponse('', ['http_code' => 500]); + }); + $transport = new MailPaceApiTransport('KEY', $client); + $transport->setPort(8984); + + $mail = new Email(); + $mail->subject('Hello!') + ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin')) + ->from(new Address('fabpot@symfony.com', 'Fabien')) + ->text('Hello There!'); + + $this->expectException(HttpTransportException::class); + $this->expectExceptionMessage('Unable to send an email: (code 500).'); + $transport->send($mail); + } + public function testTagAndMetadataHeaders() { $email = new Email(); diff --git a/src/Symfony/Component/Mailer/Bridge/MailPace/Transport/MailPaceApiTransport.php b/src/Symfony/Component/Mailer/Bridge/MailPace/Transport/MailPaceApiTransport.php index 8b578612afa8e..9489b41bb7889 100644 --- a/src/Symfony/Component/Mailer/Bridge/MailPace/Transport/MailPaceApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/MailPace/Transport/MailPaceApiTransport.php @@ -67,7 +67,20 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e } if (200 !== $statusCode) { - throw new HttpTransportException('Unable to send an email: '.$result['error'].sprintf(' (code %d).', $statusCode), $response); + $errorMessage = 'Unable to send an email: '; + if (isset($result['error'])) { + $errorMessage .= $result['error']; + } elseif (isset($result['errors'])) { + $errors = []; + foreach ($result['errors'] as $key => $val) { + $errors[] = $key.': '.implode(' & ', $val); + } + $errorMessage .= implode('; ', $errors); + } else { + $errorMessage .= 'unknown error'; + } + $errorMessage .= sprintf(' (code %d).', $statusCode); + throw new HttpTransportException($errorMessage, $response); } $sentMessage->setMessageId($result['id']); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy