[Mailer] Fix addresses management in Sendgrid API payload

This commit is contained in:
Guillaume Verstraete 2020-01-03 02:28:31 +01:00
parent 3415224479
commit 36eeba7a3a
2 changed files with 21 additions and 6 deletions

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Mailer\Bridge\Sendgrid\Tests\Transport;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridApiTransport; use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridApiTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\HttpClient\ResponseInterface;
@ -48,8 +49,8 @@ class SendgridApiTransportTest extends TestCase
public function testSend() public function testSend()
{ {
$email = new Email(); $email = new Email();
$email->from('foo@example.com') $email->from(new Address('foo@example.com', 'Ms. Foo Bar'))
->to('bar@example.com') ->to(new Address('bar@example.com', 'Mr. Recipient'))
->bcc('baz@example.com') ->bcc('baz@example.com')
->text('content'); ->text('content');
@ -73,12 +74,18 @@ class SendgridApiTransportTest extends TestCase
'json' => [ 'json' => [
'personalizations' => [ 'personalizations' => [
[ [
'to' => [['email' => 'bar@example.com']], 'to' => [[
'email' => 'bar@example.com',
'name' => 'Mr. Recipient',
]],
'subject' => null, 'subject' => null,
'bcc' => [['email' => 'baz@example.com']], 'bcc' => [['email' => 'baz@example.com']],
], ],
], ],
'from' => ['email' => 'foo@example.com'], 'from' => [
'email' => 'foo@example.com',
'name' => 'Ms. Foo Bar',
],
'content' => [ 'content' => [
['type' => 'text/plain', 'value' => 'content'], ['type' => 'text/plain', 'value' => 'content'],
], ],

View File

@ -63,11 +63,19 @@ class SendgridApiTransport extends AbstractApiTransport
private function getPayload(Email $email, Envelope $envelope): array private function getPayload(Email $email, Envelope $envelope): array
{ {
$addressStringifier = function (Address $address) {return ['email' => $address->toString()]; }; $addressStringifier = function (Address $address) {
$stringified = ['email' => $address->getAddress()];
if ($address->getName()) {
$stringified['name'] = $address->getName();
}
return $stringified;
};
$payload = [ $payload = [
'personalizations' => [], 'personalizations' => [],
'from' => ['email' => $envelope->getSender()->toString()], 'from' => $addressStringifier($envelope->getSender()),
'content' => $this->getContent($email), 'content' => $this->getContent($email),
]; ];