Skip to content

Commit fac2d8c

Browse files
Merge branch '4.4'
* 4.4: [Bridge/PhpUnit] fix looking for composer cs fix [Mailer] fixed Mailgun support when a response is not JSON as expected [4.3] Cleanup tests Cleanup tests [Finder] Prevent unintentional file locks in Windows [FrameworkBundle] Fix about command not showing .env vars [DomCrawler] Fix FileFormField PHPDoc [Mailer] Remove the default dispatcher in AbstractTransport Fix #33395 PHP 5.3 compatibility
2 parents 7c9756b + 995f952 commit fac2d8c

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

AbstractTransport.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Psr\Log\NullLogger;
16-
use Symfony\Component\EventDispatcher\EventDispatcher;
1716
use Symfony\Component\Mailer\DelayedSmtpEnvelope;
1817
use Symfony\Component\Mailer\Event\MessageEvent;
1918
use Symfony\Component\Mailer\Exception\TransportException;
@@ -35,7 +34,7 @@ abstract class AbstractTransport implements TransportInterface
3534

3635
public function __construct(EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3736
{
38-
$this->dispatcher = $dispatcher ?: new EventDispatcher();
37+
$this->dispatcher = $dispatcher;
3938
$this->logger = $logger ?: new NullLogger();
4039
}
4140

@@ -67,14 +66,17 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
6766
}
6867
}
6968

70-
$event = new MessageEvent($message, $envelope, (string) $this);
71-
$this->dispatcher->dispatch($event);
72-
$envelope = $event->getEnvelope();
69+
if (null !== $this->dispatcher) {
70+
$event = new MessageEvent($message, $envelope, (string) $this);
71+
$this->dispatcher->dispatch($event);
72+
$envelope = $event->getEnvelope();
73+
}
74+
7375
if (!$envelope->getRecipients()) {
7476
return null;
7577
}
7678

77-
$message = new SentMessage($event->getMessage(), $envelope);
79+
$message = new SentMessage($message, $envelope);
7880
$this->doSend($message);
7981

8082
$this->checkThrottling();

Mailgun/Transport/MailgunApiTransport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInte
6262
]);
6363

6464
if (200 !== $response->getStatusCode()) {
65-
$error = $response->toArray(false);
65+
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
66+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->toArray(false)['message'], $response->getStatusCode()), $response);
67+
}
6668

67-
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
69+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->getContent(false), $response->getStatusCode()), $response);
6870
}
6971

7072
return $response;

Mailgun/Transport/MailgunHttpTransport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
6565
]);
6666

6767
if (200 !== $response->getStatusCode()) {
68-
$error = $response->toArray(false);
68+
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
69+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->toArray(false)['message'], $response->getStatusCode()), $response);
70+
}
6971

70-
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
72+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->getContent(false), $response->getStatusCode()), $response);
7173
}
7274

7375
return $response;

MailgunApiTransport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInte
6262
]);
6363

6464
if (200 !== $response->getStatusCode()) {
65-
$error = $response->toArray(false);
65+
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
66+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->toArray(false)['message'], $response->getStatusCode()), $response);
67+
}
6668

67-
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
69+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->getContent(false), $response->getStatusCode()), $response);
6870
}
6971

7072
return $response;

MailgunHttpTransport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
6565
]);
6666

6767
if (200 !== $response->getStatusCode()) {
68-
$error = $response->toArray(false);
68+
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
69+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->toArray(false)['message'], $response->getStatusCode()), $response);
70+
}
6971

70-
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $error['message'], $response->getStatusCode()), $response);
72+
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->getContent(false), $response->getStatusCode()), $response);
7173
}
7274

7375
return $response;

Transport/AbstractTransport.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Psr\Log\NullLogger;
16-
use Symfony\Component\EventDispatcher\EventDispatcher;
1716
use Symfony\Component\Mailer\DelayedSmtpEnvelope;
1817
use Symfony\Component\Mailer\Event\MessageEvent;
1918
use Symfony\Component\Mailer\Exception\TransportException;
@@ -35,7 +34,7 @@ abstract class AbstractTransport implements TransportInterface
3534

3635
public function __construct(EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3736
{
38-
$this->dispatcher = $dispatcher ?: new EventDispatcher();
37+
$this->dispatcher = $dispatcher;
3938
$this->logger = $logger ?: new NullLogger();
4039
}
4140

@@ -67,14 +66,17 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
6766
}
6867
}
6968

70-
$event = new MessageEvent($message, $envelope, (string) $this);
71-
$this->dispatcher->dispatch($event);
72-
$envelope = $event->getEnvelope();
69+
if (null !== $this->dispatcher) {
70+
$event = new MessageEvent($message, $envelope, (string) $this);
71+
$this->dispatcher->dispatch($event);
72+
$envelope = $event->getEnvelope();
73+
}
74+
7375
if (!$envelope->getRecipients()) {
7476
return null;
7577
}
7678

77-
$message = new SentMessage($event->getMessage(), $envelope);
79+
$message = new SentMessage($message, $envelope);
7880
$this->doSend($message);
7981

8082
$this->checkThrottling();

0 commit comments

Comments
 (0)