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

Skip to content

Commit 3849e1c

Browse files
committed
feature #32583 [Mailer] Logger vs debug mailer (fabpot)
This PR was squashed before being merged into the 4.4 branch (closes #32583). Discussion ---------- [Mailer] Logger vs debug mailer | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a | License | MIT | Doc PR | n/a Currently, there is no way to get the network data for the HTTP calls done by the HTTP transports (which makes debugging harder). For SMTP, we do have the network data, but as logs (each SMTP command/response is its own log line which means that the logs are "polluted" and the data is not tied with the sent message). This pull request adds a `getDebug()` method on `SentMessage`. That allows to get the debug data conveniently in a standardized way (for both SMTP and HTTP transports). I have moved the SMTP logs to this new mechanism and added support for HTTP transports. Commits ------- fded3cd [Mailer] added support ffor debug info when using SMTP d2f33d2 [Mailer] added debug info for HTTP mailers
2 parents fcb3309 + fded3cd commit 3849e1c

File tree

14 files changed

+136
-65
lines changed

14 files changed

+136
-65
lines changed

src/Symfony/Component/Mailer/Bridge/Amazon/Http/Api/SesTransport.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
namespace Symfony\Component\Mailer\Bridge\Amazon\Http\Api;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SmtpEnvelope;
1717
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
1818
use Symfony\Component\Mime\Email;
1919
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
use Symfony\Contracts\HttpClient\ResponseInterface;
2122

2223
/**
2324
* @author Kevin Verschaeve
@@ -42,7 +43,7 @@ public function __construct(string $accessKey, string $secretKey, string $region
4243
parent::__construct($client, $dispatcher, $logger);
4344
}
4445

45-
protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
46+
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4647
{
4748
$date = gmdate('D, d M Y H:i:s e');
4849
$auth = sprintf('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA256,Signature=%s', $this->accessKey, $this->getSignature($date));
@@ -60,8 +61,10 @@ protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
6061
if (200 !== $response->getStatusCode()) {
6162
$error = new \SimpleXMLElement($response->getContent(false));
6263

63-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code));
64+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code), $response);
6465
}
66+
67+
return $response;
6568
}
6669

6770
private function getSignature(string $string): string

src/Symfony/Component/Mailer/Bridge/Amazon/Http/SesTransport.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
namespace Symfony\Component\Mailer\Bridge\Amazon\Http;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SentMessage;
1717
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
1818
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1919
use Symfony\Contracts\HttpClient\HttpClientInterface;
20+
use Symfony\Contracts\HttpClient\ResponseInterface;
2021

2122
/**
2223
* @author Kevin Verschaeve
@@ -41,7 +42,7 @@ public function __construct(string $accessKey, string $secretKey, string $region
4142
parent::__construct($client, $dispatcher, $logger);
4243
}
4344

44-
protected function doSend(SentMessage $message): void
45+
protected function doSendHttp(SentMessage $message): ResponseInterface
4546
{
4647
$date = gmdate('D, d M Y H:i:s e');
4748
$auth = sprintf('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA256,Signature=%s', $this->accessKey, $this->getSignature($date));
@@ -61,8 +62,10 @@ protected function doSend(SentMessage $message): void
6162
if (200 !== $response->getStatusCode()) {
6263
$error = new \SimpleXMLElement($response->getContent(false));
6364

64-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code));
65+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code), $response);
6566
}
67+
68+
return $response;
6669
}
6770

6871
private function getSignature(string $string): string

src/Symfony/Component/Mailer/Bridge/Mailchimp/Http/Api/MandrillTransport.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Http\Api;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SmtpEnvelope;
1717
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
1818
use Symfony\Component\Mime\Email;
1919
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
use Symfony\Contracts\HttpClient\ResponseInterface;
2122

2223
/**
2324
* @author Kevin Verschaeve
@@ -35,7 +36,7 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3536
parent::__construct($client, $dispatcher, $logger);
3637
}
3738

38-
protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
39+
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
3940
{
4041
$response = $this->client->request('POST', self::ENDPOINT, [
4142
'json' => $this->getPayload($email, $envelope),
@@ -44,11 +45,13 @@ protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
4445
if (200 !== $response->getStatusCode()) {
4546
$result = $response->toArray(false);
4647
if ('error' === ($result['status'] ?? false)) {
47-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $result['message'], $result['code']));
48+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $result['message'], $result['code']), $response);
4849
}
4950

50-
throw new TransportException(sprintf('Unable to send an email (code %s).', $result['code']));
51+
throw new HttpTransportException(sprintf('Unable to send an email (code %s).', $result['code']), $response);
5152
}
53+
54+
return $response;
5255
}
5356

5457
private function getPayload(Email $email, SmtpEnvelope $envelope): array

src/Symfony/Component/Mailer/Bridge/Mailchimp/Http/MandrillTransport.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Http;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SentMessage;
1717
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
1818
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1919
use Symfony\Contracts\HttpClient\HttpClientInterface;
20+
use Symfony\Contracts\HttpClient\ResponseInterface;
2021

2122
/**
2223
* @author Kevin Verschaeve
@@ -33,7 +34,7 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3334
parent::__construct($client, $dispatcher, $logger);
3435
}
3536

36-
protected function doSend(SentMessage $message): void
37+
protected function doSendHttp(SentMessage $message): ResponseInterface
3738
{
3839
$envelope = $message->getEnvelope();
3940
$response = $this->client->request('POST', self::ENDPOINT, [
@@ -48,10 +49,12 @@ protected function doSend(SentMessage $message): void
4849
if (200 !== $response->getStatusCode()) {
4950
$result = $response->toArray(false);
5051
if ('error' === ($result['status'] ?? false)) {
51-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $result['message'], $result['code']));
52+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $result['message'], $result['code']), $response);
5253
}
5354

54-
throw new TransportException(sprintf('Unable to send an email (code %s).', $result['code']));
55+
throw new HttpTransportException(sprintf('Unable to send an email (code %s).', $result['code']), $response);
5556
}
57+
58+
return $response;
5659
}
5760
}

src/Symfony/Component/Mailer/Bridge/Mailgun/Http/Api/MailgunTransport.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
namespace Symfony\Component\Mailer\Bridge\Mailgun\Http\Api;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SmtpEnvelope;
1717
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
1818
use Symfony\Component\Mime\Email;
1919
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
2020
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2121
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
2223

2324
/**
2425
* @author Kevin Verschaeve
@@ -40,7 +41,7 @@ public function __construct(string $key, string $domain, string $region = null,
4041
parent::__construct($client, $dispatcher, $logger);
4142
}
4243

43-
protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
44+
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4445
{
4546
$body = new FormDataPart($this->getPayload($email, $envelope));
4647
$headers = [];
@@ -58,8 +59,10 @@ protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
5859
if (200 !== $response->getStatusCode()) {
5960
$error = $response->toArray(false);
6061

61-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()));
62+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
6263
}
64+
65+
return $response;
6366
}
6467

6568
private function getPayload(Email $email, SmtpEnvelope $envelope): array

src/Symfony/Component/Mailer/Bridge/Mailgun/Http/MailgunTransport.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
namespace Symfony\Component\Mailer\Bridge\Mailgun\Http;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SentMessage;
1717
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
1818
use Symfony\Component\Mime\Part\DataPart;
1919
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
2020
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2121
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
2223

2324
/**
2425
* @author Kevin Verschaeve
@@ -39,7 +40,7 @@ public function __construct(string $key, string $domain, string $region = null,
3940
parent::__construct($client, $dispatcher, $logger);
4041
}
4142

42-
protected function doSend(SentMessage $message): void
43+
protected function doSendHttp(SentMessage $message): ResponseInterface
4344
{
4445
$body = new FormDataPart([
4546
'to' => implode(',', $this->stringifyAddresses($message->getEnvelope()->getRecipients())),
@@ -59,7 +60,9 @@ protected function doSend(SentMessage $message): void
5960
if (200 !== $response->getStatusCode()) {
6061
$error = $response->toArray(false);
6162

62-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()));
63+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
6364
}
65+
66+
return $response;
6467
}
6568
}

src/Symfony/Component/Mailer/Bridge/Postmark/Http/Api/PostmarkTransport.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
namespace Symfony\Component\Mailer\Bridge\Postmark\Http\Api;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SmtpEnvelope;
1717
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
1818
use Symfony\Component\Mime\Email;
1919
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
use Symfony\Contracts\HttpClient\ResponseInterface;
2122

2223
/**
2324
* @author Kevin Verschaeve
@@ -35,7 +36,7 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3536
parent::__construct($client, $dispatcher, $logger);
3637
}
3738

38-
protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
39+
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
3940
{
4041
$response = $this->client->request('POST', self::ENDPOINT, [
4142
'headers' => [
@@ -48,8 +49,10 @@ protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
4849
if (200 !== $response->getStatusCode()) {
4950
$error = $response->toArray(false);
5051

51-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error['Message'], $error['ErrorCode']));
52+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['Message'], $error['ErrorCode']), $response);
5253
}
54+
55+
return $response;
5356
}
5457

5558
private function getPayload(Email $email, SmtpEnvelope $envelope): array

src/Symfony/Component/Mailer/Bridge/Sendgrid/Http/Api/SendgridTransport.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Http\Api;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\Mailer\Exception\TransportException;
15+
use Symfony\Component\Mailer\Exception\HttpTransportException;
1616
use Symfony\Component\Mailer\SmtpEnvelope;
1717
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
1818
use Symfony\Component\Mime\Address;
1919
use Symfony\Component\Mime\Email;
2020
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2121
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
2223

2324
/**
2425
* @author Kevin Verschaeve
@@ -36,7 +37,7 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3637
parent::__construct($client, $dispatcher, $logger);
3738
}
3839

39-
protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
40+
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4041
{
4142
$response = $this->client->request('POST', self::ENDPOINT, [
4243
'json' => $this->getPayload($email, $envelope),
@@ -46,8 +47,10 @@ protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
4647
if (202 !== $response->getStatusCode()) {
4748
$errors = $response->toArray(false);
4849

49-
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', implode('; ', array_column($errors['errors'], 'message')), $response->getStatusCode()));
50+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', implode('; ', array_column($errors['errors'], 'message')), $response->getStatusCode()), $response);
5051
}
52+
53+
return $response;
5154
}
5255

5356
private function getPayload(Email $email, SmtpEnvelope $envelope): array

src/Symfony/Component/Mailer/Exception/HttpTransportException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@
1111

1212
namespace Symfony\Component\Mailer\Exception;
1313

14+
use Symfony\Contracts\HttpClient\ResponseInterface;
15+
1416
/**
1517
* @author Fabien Potencier <[email protected]>
1618
*/
1719
class HttpTransportException extends TransportException
1820
{
21+
private $response;
22+
23+
public function __construct(string $message = null, ResponseInterface $response, int $code = 0, \Exception $previous = null)
24+
{
25+
parent::__construct($message, $code, $previous);
26+
27+
$this->response = $response;
28+
}
29+
30+
public function getResponse(): ResponseInterface
31+
{
32+
return $this->response;
33+
}
1934
}

src/Symfony/Component/Mailer/SentMessage.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SentMessage
2222
private $original;
2323
private $raw;
2424
private $envelope;
25+
private $debug = '';
2526

2627
/**
2728
* @internal
@@ -48,6 +49,16 @@ public function getEnvelope(): SmtpEnvelope
4849
return $this->envelope;
4950
}
5051

52+
public function getDebug(): string
53+
{
54+
return $this->debug;
55+
}
56+
57+
public function appendDebug(string $debug): void
58+
{
59+
$this->debug .= $debug;
60+
}
61+
5162
public function toString(): string
5263
{
5364
return $this->raw->toString();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\HttpClient\HttpClient;
16+
use Symfony\Component\Mailer\Exception\HttpTransportException;
17+
use Symfony\Component\Mailer\SentMessage;
1618
use Symfony\Component\Mailer\Transport\AbstractTransport;
1719
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1820
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
use Symfony\Contracts\HttpClient\ResponseInterface;
1922

2023
/**
2124
* @author Victor Bocharsky <[email protected]>
@@ -37,4 +40,22 @@ public function __construct(HttpClientInterface $client = null, EventDispatcherI
3740

3841
parent::__construct($dispatcher, $logger);
3942
}
43+
44+
abstract protected function doSendHttp(SentMessage $message): ResponseInterface;
45+
46+
protected function doSend(SentMessage $message): void
47+
{
48+
$response = null;
49+
try {
50+
$response = $this->doSendHttp($message);
51+
} catch (HttpTransportException $e) {
52+
$response = $e->getResponse();
53+
54+
throw $e;
55+
} finally {
56+
if (null !== $response) {
57+
$message->appendDebug($response->getInfo('debug'));
58+
}
59+
}
60+
}
4061
}

0 commit comments

Comments
 (0)