[Mailer] Fix reply-to functionality in the SendgridApiTransport

This commit is contained in:
Jason Tan 2020-07-08 11:11:49 -05:00 committed by Fabien Potencier
parent ddf795390a
commit 2cf25d1055
2 changed files with 32 additions and 1 deletions

View File

@ -166,4 +166,30 @@ class SendgridApiTransportTest extends TestCase
$this->assertArrayHasKey('foo', $payload['headers']);
$this->assertEquals('bar', $payload['headers']['foo']);
}
public function testReplyTo()
{
$from = 'from@example.com';
$to = 'to@example.com';
$replyTo = 'replyto@example.com';
$email = new Email();
$email->from($from)
->to($to)
->replyTo($replyTo)
->text('content');
$envelope = new Envelope(new Address($from), [new Address($to)]);
$transport = new SendgridApiTransport('ACCESS_KEY');
$method = new \ReflectionMethod(SendgridApiTransport::class, 'getPayload');
$method->setAccessible(true);
$payload = $method->invoke($transport, $email, $envelope);
$this->assertArrayHasKey('from', $payload);
$this->assertArrayHasKey('email', $payload['from']);
$this->assertSame($from, $payload['from']['email']);
$this->assertArrayHasKey('reply_to', $payload);
$this->assertArrayHasKey('email', $payload['reply_to']);
$this->assertSame($replyTo, $payload['reply_to']['email']);
}
}

View File

@ -93,11 +93,16 @@ class SendgridApiTransport extends AbstractApiTransport
if ($emails = array_map($addressStringifier, $email->getBcc())) {
$personalization['bcc'] = $emails;
}
if ($emails = array_map($addressStringifier, $email->getReplyTo())) {
// Email class supports an array of reply-to addresses,
// but SendGrid only supports a single address
$payload['reply_to'] = $emails[0];
}
$payload['personalizations'][] = $personalization;
// these headers can't be overwritten according to Sendgrid docs
// see https://developers.pepipost.com/migration-api/new-subpage/email-send
// see https://sendgrid.api-docs.io/v3.0/mail-send/mail-send-errors#-Headers-Errors
$headersToBypass = ['x-sg-id', 'x-sg-eid', 'received', 'dkim-signature', 'content-transfer-encoding', 'from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'reply-to'];
foreach ($email->getHeaders()->all() as $name => $header) {
if (\in_array($name, $headersToBypass, true)) {