8000 bug #35227 [Mailer] Fix broken mandrill http send for recipients with… · symfony/symfony@435f4d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 435f4d5

Browse files
committed
bug #35227 [Mailer] Fix broken mandrill http send for recipients with names (vilius-g)
This PR was submitted for the 4.3 branch but it was squashed and merged into the 4.4 branch instead (closes #35227). Discussion ---------- [Mailer] Fix broken mandrill http send for recipients with names | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | As specified in https://mandrillapp.com/api/docs/messages.JSON.html#method=send-raw, Mandrill API expects array of email addresses for `to` parameter. Commits ------- fbfe1ed [Mailer] Fix broken mandrill http send for recipients with names
2 parents 7dc5d64 + fbfe1ed commit 435f4d5

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Http;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Component\Mailer\Exception\TransportException;
17+
use Symfony\Component\Mailer\SentMessage;
18+
use Symfony\Component\Mailer\SmtpEnvelope;
19+
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
20+
use Symfony\Component\Mime\Address;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Kevin Verschaeve
25+
*
26+
* @experimental in 4.3
27+
*/
28+
class MandrillTransport extends AbstractHttpTransport
29+
{
30+
private const ENDPOINT = 'https://mandrillapp.com/api/1.0/messages/send-raw.json';
31+
private $key;
32+
33+
public function __construct(string $key, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
34+
{
35+
$this->key = $key;
36+
37+
parent::__construct($client, $dispatcher, $logger);
38+
}
39+
40+
protected function doSend(SentMessage $message): void
41+
{
42+
$envelope = $message->getEnvelope();
43+
$response = $this->client->request('POST', self::ENDPOINT, [
44+
'json' => [
45+
'key' => $this->key,
46+
'to' => $this->getRecipients($envelope),
47+
'from_email' => $envelope->getSender()->getAddress(),
48+
'raw_message' => $message->toString(),
49+
],
50+
]);
51+
52+
if (200 !== $response->getStatusCode()) {
53+
$result = $response->toArray(false);
54+
if ('error' === ($result['status'] ?? false)) {
55+
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $result['message'], $result['code']));
56+
}
57+
58+
throw new TransportException(sprintf('Unable to send an email (code %s).', $result['code']));
59+
}
60+
}
61+
62+
/**
63+
* @return string[]
64+
*/
65+
private function getRecipients(SmtpEnvelope $envelope): array
66+
{
67+
return array_map(function (Address $recipient): string {
68+
return $recipient->getAddress();
69+
}, $envelope->getRecipients());
70+
}
71+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Transport;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\Mailer\Envelope;
1516
use Symfony\Component\Mailer\Exception\HttpTransportException;
1617
use Symfony\Component\Mailer\SentMessage;
1718
use Symfony\Component\Mailer\Transport\AbstractHttpTransport;
19+
use Symfony\Component\Mime\Address;
1820
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1921
use Symfony\Contracts\HttpClient\HttpClientInterface;
2022
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -45,7 +47,7 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
4547
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/1.0/messages/send-raw.json', [
4648
'json' => [
4749
'key' => $this->key,
48-
'to' => $this->stringifyAddresses($envelope->getRecipients()),
50+
'to' => $this->getRecipients($envelope->getRecipients()),
4951
'from_email' => $envelope->getSender()->toString(),
5052
'raw_message' => $message->toString(),
5153
],
@@ -69,4 +71,14 @@ private function getEndpoint(): ?string
6971
{
7072
return ($this->host ?: self::HOST).($this->port ? ':'.$this->port : '');
7173
}
74+
75+
/**
76+
* @return string[]
77+
*/
78+
private function getRecipients(Envelope $envelope): array
79+
{
80+
return array_map(function (Address $recipient): string {
81+
return $recipient->getAddress();
82+
}, $envelope->getRecipients());
83+
}
7284
}

0 commit comments

Comments
 (0)
0