Skip to content

[Mailer] Logger vs debug mailer #32583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
namespace Symfony\Component\Mailer\Bridge\Amazon\Http\Api;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SmtpEnvelope;
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code));
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code), $response);
}

return $response;
}

private function getSignature(string $string): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace Symfony\Component\Mailer\Bridge\Amazon\Http;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code));
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code), $response);
}

return $response;
}

private function getSignature(string $string): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Http\Api;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SmtpEnvelope;
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email (code %s).', $result['code']));
throw new HttpTransportException(sprintf('Unable to send an email (code %s).', $result['code']), $response);
}

return $response;
}

private function getPayload(Email $email, SmtpEnvelope $envelope): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Http;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email (code %s).', $result['code']));
throw new HttpTransportException(sprintf('Unable to send an email (code %s).', $result['code']), $response);
}

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
namespace Symfony\Component\Mailer\Bridge\Mailgun\Http\Api;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SmtpEnvelope;
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()));
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
}

return $response;
}

private function getPayload(Email $email, SmtpEnvelope $envelope): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
namespace Symfony\Component\Mailer\Bridge\Mailgun\Http;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()));
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
}

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
namespace Symfony\Component\Mailer\Bridge\Postmark\Http\Api;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SmtpEnvelope;
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error['Message'], $error['ErrorCode']));
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['Message'], $error['ErrorCode']), $response);
}

return $response;
}

private function getPayload(Email $email, SmtpEnvelope $envelope): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Http\Api;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SmtpEnvelope;
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

throw new TransportException(sprintf('Unable to send an email: %s (code %s).', implode('; ', array_column($errors['errors'], 'message')), $response->getStatusCode()));
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', implode('; ', array_column($errors['errors'], 'message')), $response->getStatusCode()), $response);
}

return $response;
}

private function getPayload(Email $email, SmtpEnvelope $envelope): array
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Mailer/Exception/HttpTransportException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,24 @@

namespace Symfony\Component\Mailer\Exception;

use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class HttpTransportException extends TransportException
{
private $response;

public function __construct(string $message = null, ResponseInterface $response, int $code = 0, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);

$this->response = $response;
}

public function getResponse(): ResponseInterface
{
return $this->response;
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/Mailer/SentMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SentMessage
private $original;
private $raw;
private $envelope;
private $debug = '';

/**
* @internal
Expand All @@ -48,6 +49,16 @@ public function getEnvelope(): SmtpEnvelope
return $this->envelope;
}

public function getDebug(): string
{
return $this->debug;
}

public function appendDebug(string $debug): void
{
$this->debug .= $debug;
}

public function toString(): string
{
return $this->raw->toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @author Victor Bocharsky <victor@symfonycasts.com>
Expand All @@ -37,4 +40,22 @@ public function __construct(HttpClientInterface $client = null, EventDispatcherI

parent::__construct($dispatcher, $logger);
}

abstract protected function doSendHttp(SentMessage $message): ResponseInterface;

protected function doSend(SentMessage $message): void
{
$response = null;
try {
$response = $this->doSendHttp($message);
} catch (HttpTransportException $e) {
$response = $e->getResponse();

throw $e;
} finally {
if (null !== $response) {
$message->appendDebug($response->getInfo('debug'));
}
}
}
}
Loading