Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 728e9d3

Browse files
committed
[Mailer] add ability to set socket/http timeout via dsn
1 parent aa9ccf8 commit 728e9d3

16 files changed

+84
-35
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6161
'Content-Type' => 'application/x-www-form-urlencoded',
6262
],
6363
'body' => $payload = $this->getPayload($email, $envelope),
64+
'timeout' => $this->timeout * 1000,
6465
]);
6566

6667
$result = new \SimpleXMLElement($response->getContent(false));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
6161
'Action' => 'SendRawEmail',
6262
'RawMessage.Data' => base64_encode($message->toString()),
6363
],
64+
'timeout' => $this->timeout * 1000,
6465
]);
6566

6667
$result = new \SimpleXMLElement($response->getContent(false));

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ public function create(Dsn $dsn): TransportInterface
3131
$port = $dsn->getPort();
3232

3333
if ('ses+api' === $scheme) {
34-
return (new SesApiTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
34+
$transport = (new SesApiTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
35+
} elseif ('ses+https' === $scheme || 'ses' === $scheme) {
36+
$transport = (new SesHttpTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
37+
} elseif ('ses+smtp' === $scheme || 'ses+smtps' === $scheme) {
38+
$transport = new SesSmtpTransport($user, $password, $region, $this->dispatcher, $this->logger);
39+
} else {
40+
throw new UnsupportedSchemeException($dsn, 'ses', $this->getSupportedSchemes());
3541
}
3642

37-
if ('ses+https' === $scheme || 'ses' === $scheme) {
38-
return (new SesHttpTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
43+
if ($timeout = $dsn->getOption('timeout')) {
44+
$transport->setTimeout((int) $timeout);
3945
}
4046

41-
if ('ses+smtp' === $scheme || 'ses+smtps' === $scheme) {
42-
return new SesSmtpTransport($user, $password, $region, $this->dispatcher, $this->logger);
43-
}
44-
45-
throw new UnsupportedSchemeException($dsn, 'ses', $this->getSupportedSchemes());
47+
return $transport;
4648
}
4749

4850
protected function getSupportedSchemes(): array

src/Symfony/Component/Mailer/Bridge/Google/Transport/GmailTransportFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ final class GmailTransportFactory extends AbstractTransportFactory
2424
public function create(Dsn $dsn): TransportInterface
2525
{
2626
if (\in_array($dsn->getScheme(), $this->getSupportedSchemes())) {
27-
return new GmailSmtpTransport($this->getUser($dsn), $this->getPassword($dsn), $this->dispatcher, $this->logger);
27+
$transport = new GmailSmtpTransport($this->getUser($dsn), $this->getPassword($dsn), $this->dispatcher, $this->logger);
28+
29+
if ($timeout = $dsn->getOption('timeout')) {
30+
$transport->setTimeout((int) $timeout);
31+
}
32+
33+
return $transport;
2834
}
2935

3036
throw new UnsupportedSchemeException($dsn, 'gmail', $this->getSupportedSchemes());

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
4646
{
4747
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/1.0/messages/send.json', [
4848
'json' => $this->getPayload($email, $envelope),
49+
'timeout' => $this->timeout * 1000,
4950
]);
5051

5152
$result = $response->toArray(false);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
4949
'from_email' => $envelope->getSender()->toString(),
5050
'raw_message' => $message->toString(),
5151
],
52+
'timeout' => $this->timeout * 1000,
5253
]);
5354

5455
$result = $response->toArray(false);

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@ public function create(Dsn $dsn): TransportInterface
2929
$port = $dsn->getPort();
3030

3131
if ('mandrill+api' === $scheme) {
32-
return (new MandrillApiTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
33-
}
32+
$transport = (new MandrillApiTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
33+
} elseif ('mandrill+https' === $scheme || 'mandrill' === $scheme) {
34+
$transport = (new MandrillHttpTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
35+
} elseif ('mandrill+smtp' === $scheme || 'mandrill+smtps' === $scheme) {
36+
$password = $this->getPassword($dsn);
3437

35-
if ('mandrill+https' === $scheme || 'mandrill' === $scheme) {
36-
return (new MandrillHttpTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
38+
$transport = new MandrillSmtpTransport($user, $password, $this->dispatcher, $this->logger);
39+
} else {
40+
throw new UnsupportedSchemeException($dsn, 'mandrill', $this->getSupportedSchemes());
3741
}
3842

39-
if ('mandrill+smtp' === $scheme || 'mandrill+smtps' === $scheme) {
40-
$password = $this->getPassword($dsn);
41-
42-
return new MandrillSmtpTransport($user, $password, $this->dispatcher, $this->logger);
43+
if ($timeout = $dsn->getOption('timeout')) {
44+
$transport->setTimeout((int) $timeout);
4345
}
4446

45-
throw new UnsupportedSchemeException($dsn, 'mandrill', $this->getSupportedSchemes());
47+
return $transport;
4648
}
4749

4850
protected function getSupportedSchemes(): array

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6060
'auth_basic' => 'api:'.$this->key,
6161
'headers' => $headers,
6262
'body' => $body->bodyToIterable(),
63+
'timeout' => $this->timeout * 1000,
6364
]);
6465

6566
$result = $response->toArray(false);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
6262
'auth_basic' => 'api:'.$this->key,
6363
'headers' => $headers,
6464
'body' => $body->bodyToIterable(),
65+
'timeout' => $this->timeout * 1000,
6566
]);
6667

6768
$result = $response->toArray(false);

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ public function create(Dsn $dsn): TransportInterface
3131
$port = $dsn->getPort();
3232

3333
if ('mailgun+api' === $scheme) {
34-
return (new MailgunApiTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
34+
$transport = (new MailgunApiTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
35+
} elseif ('mailgun+https' === $scheme || 'mailgun' === $scheme) {
36+
$transport = (new MailgunHttpTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
37+
} elseif ('mailgun+smtp' === $scheme || 'mailgun+smtps' === $scheme) {
38+
$transport = new MailgunSmtpTransport($user, $password, $region, $this->dispatcher, $this->logger);
39+
} else {
40+
throw new UnsupportedSchemeException($dsn, 'mailgun', $this->getSupportedSchemes());
3541
}
3642

37-
if ('mailgun+https' === $scheme || 'mailgun' === $scheme) {
38-
return (new MailgunHttpTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
43+
if ($timeout = $dsn->getOption('timeout')) {
44+
$transport->setTimeout((int) $timeout);
3945
}
4046

41-
if ('mailgun+smtp' === $scheme || 'mailgun+smtps' === $scheme) {
42-
return new MailgunSmtpTransport($user, $password, $region, $this->dispatcher, $this->logger);
43-
}
44-
45-
throw new UnsupportedSchemeException($dsn, 'mailgun', $this->getSupportedSchemes());
47+
return $transport;
4648
}
4749

4850
protected function getSupportedSchemes(): array

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
5050
'X-Postmark-Server-Token' => $this->key,
5151
],
5252
'json' => $this->getPayload($email, $envelope),
53+
'timeout' => $this->timeout * 1000,
5354
]);
5455

5556
$result = $response->toArray(false);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ public function create(Dsn $dsn): TransportInterface
3030
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
3131
$port = $dsn->getPort();
3232

33-
return (new PostmarkApiTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
33+
$transport = (new PostmarkApiTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
34+
} elseif ('postmark+smtp' === $scheme || 'postmark+smtps' === $scheme || 'postmark' === $scheme) {
35+
$transport = new PostmarkSmtpTransport($user, $this->dispatcher, $this->logger);
36+
} else {
37+
throw new UnsupportedSchemeException($dsn, 'postmark', $this->getSupportedSchemes());
3438
}
3539

36-
if ('postmark+smtp' === $scheme || 'postmark+smtps' === $scheme || 'postmark' === $scheme) {
37-
return new PostmarkSmtpTransport($user, $this->dispatcher, $this->logger);
40+
if ($timeout = $dsn->getOption('timeout')) {
41+
$transport->setTimeout((int) $timeout);
3842
}
3943

40-
throw new UnsupportedSchemeException($dsn, 'postmark', $this->getSupportedSchemes());
44+
return $transport;
4145
}
4246

4347
protected function getSupportedSchemes(): array

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
4848
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v3/mail/send', [
4949
'json' => $this->getPayload($email, $envelope),
5050
'auth_bearer' => $this->key,
51+
'timeout' => $this->timeout * 1000,
5152
]);
5253

5354
if (202 !== $response->getStatusCode()) {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ public function create(Dsn $dsn): TransportInterface
2929
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
3030
$port = $dsn->getPort();
3131

32-
return (new SendgridApiTransport($key, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
32+
$transport = (new SendgridApiTransport($key, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
33+
} elseif ('sendgrid+smtp' === $dsn->getScheme() || 'sendgrid+smtps' === $dsn->getScheme() || 'sendgrid' === $dsn->getScheme()) {
34+
$transport = new SendgridSmtpTransport($key, $this->dispatcher, $this->logger);
35+
} else {
36+
throw new UnsupportedSchemeException($dsn, 'sendgrid', $this->getSupportedSchemes());
3337
}
3438

35-
if ('sendgrid+smtp' === $dsn->getScheme() || 'sendgrid+smtps' === $dsn->getScheme() || 'sendgrid' === $dsn->getScheme()) {
36-
return new SendgridSmtpTransport($key, $this->dispatcher, $this->logger);
39+
if ($timeout = $dsn->getOption('timeout')) {
40+
$transport->setTimeout((int) $timeout);
3741
}
3842

39-
throw new UnsupportedSchemeException($dsn, 'sendgrid', $this->getSupportedSchemes());
43+
return $transport;
4044
}
4145

4246
protected function getSupportedSchemes(): array

src/Symfony/Component/Mailer/Transport/AbstractHttpTransport.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class AbstractHttpTransport extends AbstractTransport
2727
protected $host;
2828
protected $port;
2929
protected $client;
30+
protected $timeout = 5;
3031

3132
public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3233
{
@@ -42,6 +43,16 @@ public function __construct(HttpClientInterface $client = null, EventDispatcherI
4243
parent::__construct($dispatcher, $logger);
4344
}
4445

46+
/**
47+
* @return $this
48+
*/
49+
public function setTimeout(int $seconds)
50+
{
51+
$this->timeout = $seconds;
52+
53+
return $this;
54+
}
55+
4556
/**
4657
* @return $this
4758
*/

src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public function getStream(): AbstractStream
5050
return $this->stream;
5151
}
5252

53+
/**
54+
* @return $this
55+
*/
56+
public function setTimeout(int $seconds)
57+
{
58+
$this->stream->setTimeout($seconds);
59+
60+
return $this;
61+
}
62+
5363
/**
5464
* Sets the maximum number of messages to send before re-starting the transport.
5565
*
@@ -261,7 +271,7 @@ private function assertResponseCode(string $response, array $codes): void
261271
throw new TransportException(sprintf('Expected response code "%s" but got an empty response.', implode('/', $codes)));
262272
}
263273

264-
list($code) = sscanf($response, '%3d');
274+
[$code] = sscanf($response, '%3d');
265275
$valid = \in_array($code, $codes);
266276

267277
if (!$valid) {

0 commit comments

Comments
 (0)