Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/Mailer/Bridge/Mailchimp/Http/MandrillTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Http; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Mailer\Exception\TransportException; | ||
use Symfony\Component\Mailer\SentMessage; | ||
use Symfony\Component\Mailer\SmtpEnvelope; | ||
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Kevin Verschaeve | ||
* | ||
* @experimental in 4.3 | ||
*/ | ||
class MandrillTransport extends AbstractHttpTransport | ||
{ | ||
private const ENDPOINT = 'https://mandrillapp.com/api/1.0/messages/send-raw.json'; | ||
private $key; | ||
|
||
public function __construct(string $key, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null) | ||
{ | ||
$this->key = $key; | ||
|
||
parent::__construct($client, $dispatcher, $logger); | ||
} | ||
|
||
protected function doSend(SentMessage $message): void | ||
{ | ||
$envelope = $message->getEnvelope(); | ||
$response = $this->client->request('POST', self::ENDPOINT, [ | ||
'json' => [ | ||
'key' => $this->key, | ||
'to' => $this->getRecipients($envelope), | ||
'from_email' => $envelope->getSender()->getAddress(), | ||
'raw_message' => $message->toString(), | ||
], | ||
]); | ||
|
||
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 TransportException(sprintf('Unable to send an email (code %s).', $result['code'])); | ||
} | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
private function getRecipients(SmtpEnvelope $envelope): array | ||
{ | ||
return array_map(function (Address $recipient): string { | ||
return $recipient->getAddress(); | ||
}, $envelope->getRecipients()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters