10000 [Mailer] Make sure Http TransportException is not leaking · symfony/symfony@82a061e · GitHub
[go: up one dir, main page]

Skip to content

Commit 82a061e

Browse files
committed
[Mailer] Make sure Http TransportException is not leaking
1 parent e7faee4 commit 82a061e

File tree

10 files changed

+80
-15
lines changed

10 files changed

+80
-15
lines changed

src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesApiTransport.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Mime\Address;
2020
use Symfony\Component\Mime\Email;
2121
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2223
use Symfony\Contracts\HttpClient\HttpClientInterface;
2324
use Symfony\Contracts\HttpClient\ResponseInterface;
2425

@@ -66,8 +67,14 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6667
'body' => $payload = $this->getPayload($email, $envelope),
6768
]);
6869

70+
try {
71+
$statusCode = $response->getStatusCode();
72+
} catch (TransportExceptionInterface $e) {
73+
throw new HttpTransportException('Could not reach the remote Amazon server', $response, 0, $e);
74+
}
75+
6976
$result = new \SimpleXMLElement($response->getContent(false));
70-
if (200 !== $response->getStatusCode()) {
77+
if (200 !== $statusCode) {
7178
throw new HttpTransportException('Unable to send an email: '.$result->Error->Message.sprintf(' (code %d).', $result->Error->Code), $response);
7279
}
7380

src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesHttpTransport.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Mailer\Transport\AbstractHttpTransport;
1818
use Symfony\Component\Mime\Message;
1919
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2021
use Symfony\Contracts\HttpClient\HttpClientInterface;
2122
use Symfony\Contracts\HttpClient\ResponseInterface;
2223

@@ -79,8 +80,14 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
7980

8081
$response = $this->client->request('POST', 'https://'.$this->getEndpoint(), $request);
8182

83+
try {
84+
$statusCode = $response->getStatusCode();
85+
} catch (TransportExceptionInterface $e) {
86+
throw new HttpTransportException('Could not reach the remote Amazon server', $response, 0, $e);
87+
}
88+
8289
$result = new \SimpleXMLElement($response->getContent(false));
83-
if (200 !== $response->getStatusCode()) {
90+
if (200 !== $statusCode) {
8491
throw new HttpTransportException('Unable to send an email: '.$result->Error->Message.sprintf(' (code %d).', $result->Error->Code), $response);
8592
}
8693

src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillApiTransport.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
2121
use Symfony\Component\Mime\Email;
2222
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
23+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2324
use Symfony\Contracts\HttpClient\HttpClientInterface;
2425
use Symfony\Contracts\HttpClient\ResponseInterface;
2526

@@ -50,8 +51,14 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
5051
'json' => $this->getPayload($email, $envelope B41A ),
5152
]);
5253

54+
try {
55+
$statusCode = $response->getStatusCode();
56+
} catch (TransportExceptionInterface $e) {
57+
throw new HttpTransportException('Could not reach the remote Mandrill server', $response, 0, $e);
58+
}
59+
5360
$result = $response->toArray(false);
54-
if (200 !== $response->getStatusCode()) {
61+
if (200 !== $statusCode) {
5562
if ('error' === ($result['status'] ?? false)) {
5663
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $result['code']), $response);
5764
}

src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillHttpTransport.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Mailer\Transport\AbstractHttpTransport;
1818
use Symfony\Component\Mime\Address;
1919
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2021
use Symfony\Contracts\HttpClient\HttpClientInterface;
2122
use Symfony\Contracts\HttpClient\ResponseInterface;
2223

@@ -57,8 +58,14 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
5758
],
5859
]);
5960

61+
try {
62+
$statusCode = $response->getStatusCode();
63+
} catch (TransportExceptionInterface $e) {
64+
throw new HttpTransportException('Could not reach the remote Mandrill server', $response, 0, $e);
65+
}
66+
6067
$result = $response->toArray(false);
61-
if (200 !== $response->getStatusCode()) {
68+
if (200 !== $statusCode) {
6269
if ('error' === ($result['status'] ?? false)) {
6370
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $result['code']), $response);
6471
}

src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Mime\Email;
2222
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
2323
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
24+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2425
use Symfony\Contracts\HttpClient\HttpClientInterface;
2526
use Symfony\Contracts\HttpClient\ResponseInterface;
2627

@@ -64,13 +65,19 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6465
'body' => $body->bodyToIterable(),
6566
]);
6667

67-
if (200 !== $response->getStatusCode()) {
68+
try {
69+
$statusCode = $response->getStatusCode();
70+
} catch (TransportExceptionInterface $e) {
71+
throw new HttpTransportException('Could not reach the remote Mailgun server', $response, 0, $e);
72+
}
73+
74+
if (200 !== $statusCode) {
6875
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
6976
$result = $response->toArray(false);
70-
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $response->getStatusCode()), $response);
77+
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $statusCode), $response);
7178
}
7279

73-
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $response->getStatusCode()), $response);
80+
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $statusCode), $response);
7481
}
7582

7683
// The assumption here is that all 200 responses are "application/json", so it's safe to call "toArray".

src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunHttpTransport.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Mime\Part\DataPart;
1919
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
2020
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2122
use Symfony\Contracts\HttpClient\HttpClientInterface;
2223
use Symfony\Contracts\HttpClient\ResponseInterface;
2324

@@ -66,13 +67,18 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
6667
'body' => $body->bodyToIterable(),
6768
]);
6869

69-
$result = $response->toArray(false);
70+
try {
71+
$result = $response->toArray(false);
72+
} catch (TransportExceptionInterface $e) {
73+
throw new HttpTransportException('Could not reach the remote Mailgun server', $response, 0, $e);
74+
}
75+
7076
if (200 !== $response->getStatusCode()) {
7177
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
72-
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $response->getStatusCode()), $response);
78+
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $statusCode), $response);
7379
}
7480

75-
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $response->getStatusCode()), $response);
81+
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $statusCode), $response);
7682
}
7783

7884
$message->setMessageId($result['id']);

src/Symfony/Component/Mailer/Bridge/Mailjet/Transport/MailjetApiTransport.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Mime\Address;
2121
use Symfony\Component\Mime\Email;
2222
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
23+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2324
use Symfony\Contracts\HttpClient\HttpClientInterface;
2425
use Symfony\Contracts\HttpClient\ResponseInterface;
2526

@@ -63,7 +64,11 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6364
'json' => $this->getPayload($email, $envelope),
6465
]);
6566

66-
$result = $response->toArray(false);
67+
try {
68+
$result = $response->toArray(false);
69+
} catch (TransportExceptionInterface $e) {
70+
throw new HttpTransportException('Could not reach the remote Mailjet server', $response, 0, $e);
71+
}
6772

6873
if (200 !== $response->getStatusCode()) {
6974
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {

src/Symfony/Component/Mailer/Bridge/Postmark/Transport/PostmarkApiTransport.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
2121
use Symfony\Component\Mime\Email;
2222
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
23+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2324
use Symfony\Contracts\HttpClient\HttpClientInterface;
2425
use Symfony\Contracts\HttpClient\ResponseInterface;
2526

@@ -54,7 +55,12 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
5455
'json' => $this->getPayload($email, $envelope),
5556
]);
5657

57-
$result = $response->toArray(false);
58+
try {
59+
$result = $response->toArray(false);
60+
} catch (TransportExceptionInterface $e) {
61+
throw new HttpTransportException('Could not reach the remote Postmark server', $response, 0, $e);
62+
}
63+
5864
if (200 !== $response->getStatusCode()) {
5965
throw new HttpTransportException('Unable to send an email: '.$result['Message'].sprintf(' (code %d).', $result['ErrorCode']), $response);
6066
}

src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridApiTransport.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Mime\Address;
2020
use Symfony\Component\Mime\Email;
2121
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2223
use Symfony\Contracts\HttpClient\HttpClientInterface;
2324
use Symfony\Contracts\HttpClient\ResponseInterface;
2425

@@ -50,10 +51,16 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
5051
'auth_bearer' => $this->key,
5152
]);
5253

53-
if (202 !== $response->getStatusCode()) {
54+
try {
55+
$statusCode = $response->getStatusCode();
56+
} catch (TransportExceptionInterface $e) {
57+
throw new HttpTransportException('Could not reach the remote Sendgrid server', $response, 0, $e);
58+
}
59+
60+
if (202 !== $statusCode) {
5461
$errors = $response->toArray(false);
5562

56-
throw new HttpTransportException('Unable to send an email: '.implode('; ', array_column($errors['errors'], 'message')).sprintf(' (code %d).', $response->getStatusCode()), $response);
63+
throw new HttpTransportException('Unable to send an email: '.implode('; ', array_column($errors['errors'], 'message')).sprintf(' (code %d).', $statusCode), $response);
5764
}
5865

5966
$sentMessage->setMessageId($response->getHeaders(false)['x-message-id'][0]);

src/Symfony/Component/Mailer/Bridge/Sendinblue/Transport/SendinblueApiTransport.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Mime\Email;
2323
use Symfony\Component\Mime\Header\Headers;
2424
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
25+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2526
use Symfony\Contracts\HttpClient\HttpClientInterface;
2627
use Symfony\Contracts\HttpClient\ResponseInterface;
2728

@@ -53,7 +54,12 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
5354
],
5455
]);
5556

56-
$result = $response->toArray(false);
57+
try {
58+
$result = $response->toArray(false);
59+
} catch (TransportExceptionInterface $e) {
60+
throw new HttpTransportException('Could not reach the remote Sendinblue server', $response, 0, $e);
61+
}
62+
5763
if (201 !== $response->getStatusCode()) {
5864
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $response->getStatusCode()), $response);
5965
}

0 commit comments

Comments
 (0)
0