Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3413e23

Browse filesBrowse files
bug #42184 [Mailer] Make sure Http TransportException is not leaking (Nyholm)
This PR was merged into the 4.4 branch. Discussion ---------- [Mailer] Make sure Http TransportException is not leaking | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | We dont want the mailer to throw exceptions from the http-client component. This will make sure to catch such exceptions and rethrow the proper HttpTransportException from the Mailer component. Commits ------- 1681bc1 [Mailer] Make sure Http TransportException is not leaking
2 parents 947203a + 1681bc1 commit 3413e23
Copy full SHA for 3413e23

File tree

8 files changed

+78
-21
lines changed
Filter options

8 files changed

+78
-21
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesApiTransport.php
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
1919
use Symfony\Component\Mime\Email;
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

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

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesHttpTransport.php
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Mailer\SentMessage;
1717
use Symfony\Component\Mailer\Transport\AbstractHttpTransport;
1818
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
19+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021
use Symfony\Contracts\HttpClient\ResponseInterface;
2122

@@ -69,8 +70,15 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
6970

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

72-
$result = new \SimpleXMLElement($response->getContent(false));
73-
if (200 !== $response->getStatusCode()) {
73+
try {
74+
$statusCode = $response->getStatusCode();
75+
$content = $response->getContent(false);
76+
} catch (TransportExceptionInterface $e) {
77+
throw new HttpTransportException('Could not reach the remote Amazon server.', $response, 0, $e);
78+
}
79+
80+
$result = new \SimpleXMLElement($content);
81+
if (200 !== $statusCode) {
7482
throw new HttpTransportException('Unable to send an email: '.$result->Error->Message.sprintf(' (code %d).', $result->Error->Code), $response);
7583
}
7684

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillApiTransport.php
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
1919
use Symfony\Component\Mime\Email;
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

@@ -48,8 +49,14 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
4849
'json' => $this->getPayload($email, $envelope),
4950
]);
5051

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillHttpTransport.php
+9-2Lines changed: 9 additions & 2 deletions
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

@@ -55,8 +56,14 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
5556
],
5657
]);
5758

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Mime\Email;
2020
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
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

@@ -62,13 +63,19 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6263
'body' => $body->bodyToIterable(),
6364
]);
6465

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

71-
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $response->getStatusCode()), $response);
78+
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $statusCode), $response);
7279
}
7380

7481
$sentMessage->setMessageId($result['id']);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunHttpTransport.php
+11-4Lines changed: 11 additions & 4 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

@@ -64,13 +65,19 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
6465
'body' => $body->bodyToIterable(),
6566
]);
6667

67-
$result = $response->toArray(false);
68-
if (200 !== $response->getStatusCode()) {
68+
try {
69+
$statusCode = $response->getStatusCode();
70+
$result = $response->toArray(false);
71+
} catch (TransportExceptionInterface $e) {
72+
throw new HttpTransportException('Could not reach the remote Mailgun server.', $response, 0, $e);
73+
}
74+
75+
if (200 !== $statusCode) {
6976
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
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
$message->setMessageId($result['id']);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Postmark/Transport/PostmarkApiTransport.php
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
1919
use Symfony\Component\Mime\Email;
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

@@ -52,8 +53,14 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
5253
'json' => $this->getPayload($email, $envelope),
5354
]);
5455

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridApiTransport.php
+9-3Lines changed: 9 additions & 3 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,15 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
5051
'auth_bearer' => $this->key,
5152
]);
5253

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

56-
throw new HttpTransportException('Unable to send an email: '.implode('; ', array_column($errors['errors'], 'message')).sprintf(' (code %d).', $response->getStatusCode()), $response);
61+
if (202 !== $statusCode) {
62+
throw new HttpTransportException('Unable to send an email: '.implode('; ', array_column($result['errors'], 'message')).sprintf(' (code %d).', $statusCode), $response);
5763
}
5864

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.