[Mailer] Mailjet - Allow using Reply-To with Mailjet API

This commit is contained in:
Thibault RICHARD 2020-09-13 19:04:41 +02:00 committed by Fabien Potencier
parent 28edd705e8
commit 1ff3e293e0
2 changed files with 36 additions and 2 deletions

View File

@ -38,7 +38,8 @@ class MailjetApiTransportTest extends TestCase
public function testPayloadFormat() public function testPayloadFormat()
{ {
$email = (new Email()) $email = (new Email())
->subject('Sending email to mailjet API'); ->subject('Sending email to mailjet API')
->replyTo(new Address('qux@example.com', 'Qux'));
$email->getHeaders() $email->getHeaders()
->addTextHeader('X-authorized-header', 'authorized') ->addTextHeader('X-authorized-header', 'authorized')
->addTextHeader('X-MJ-TemplateLanguage', 'forbidden'); // This header is forbidden ->addTextHeader('X-MJ-TemplateLanguage', 'forbidden'); // This header is forbidden
@ -76,5 +77,31 @@ class MailjetApiTransportTest extends TestCase
$this->assertEquals('', $recipients[0]['Name']); // For Recipients, even if the name is filled, it is empty $this->assertEquals('', $recipients[0]['Name']); // For Recipients, even if the name is filled, it is empty
$this->assertEquals('baz@example.com', $recipients[1]['Email']); $this->assertEquals('baz@example.com', $recipients[1]['Email']);
$this->assertEquals('', $recipients[1]['Name']); $this->assertEquals('', $recipients[1]['Name']);
$this->assertArrayHasKey('ReplyTo', $message);
$replyTo = $message['ReplyTo'];
$this->assertIsArray($replyTo);
$this->assertEquals('qux@example.com', $replyTo['Email']);
$this->assertEquals('Qux', $replyTo['Name']);
}
public function testReplyTo()
{
$from = 'foo@example.com';
$to = 'bar@example.com';
$email = new Email();
$email
->from($from)
->to($to)
->replyTo(new Address('qux@example.com', 'Qux'), new Address('quux@example.com', 'Quux'));
$envelope = new Envelope(new Address($from), [new Address($to)]);
$transport = new MailjetApiTransport(self::USER, self::PASSWORD);
$method = new \ReflectionMethod(MailjetApiTransport::class, 'getPayload');
$method->setAccessible(true);
$this->expectExceptionMessage('Mailjet\'s API only supports one Reply-To email, 2 given.');
$method->invoke($transport, $email, $envelope);
} }
} }

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Mailer\Bridge\Mailjet\Transport;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException; use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractApiTransport; use Symfony\Component\Mailer\Transport\AbstractApiTransport;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
@ -29,7 +30,7 @@ class MailjetApiTransport extends AbstractApiTransport
private const FORBIDDEN_HEADERS = [ private const FORBIDDEN_HEADERS = [
'Date', 'X-CSA-Complaints', 'Message-Id', 'X-Mailjet-Campaign', 'X-MJ-StatisticsContactsListID', 'Date', 'X-CSA-Complaints', 'Message-Id', 'X-Mailjet-Campaign', 'X-MJ-StatisticsContactsListID',
'DomainKey-Status', 'Received-SPF', 'Authentication-Results', 'Received', 'X-Mailjet-Prio', 'DomainKey-Status', 'Received-SPF', 'Authentication-Results', 'Received', 'X-Mailjet-Prio',
'From', 'Sender', 'Subject', 'To', 'Cc', 'Bcc', 'Return-Path', 'Delivered-To', 'DKIM-Signature', 'From', 'Sender', 'Subject', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path', 'Delivered-To', 'DKIM-Signature',
'X-Feedback-Id', 'X-Mailjet-Segmentation', 'List-Id', 'X-MJ-MID', 'X-MJ-ErrorMessage', 'X-Feedback-Id', 'X-Mailjet-Segmentation', 'List-Id', 'X-MJ-MID', 'X-MJ-ErrorMessage',
'X-MJ-TemplateErrorDeliver', 'X-MJ-TemplateErrorReporting', 'X-MJ-TemplateLanguage', 'X-MJ-TemplateErrorDeliver', 'X-MJ-TemplateErrorReporting', 'X-MJ-TemplateLanguage',
'X-Mailjet-Debug', 'User-Agent', 'X-Mailer', 'X-MJ-CustomID', 'X-MJ-EventPayload', 'X-MJ-Vars', 'X-Mailjet-Debug', 'User-Agent', 'X-Mailer', 'X-MJ-CustomID', 'X-MJ-EventPayload', 'X-MJ-Vars',
@ -106,6 +107,12 @@ class MailjetApiTransport extends AbstractApiTransport
if ($emails = $email->getBcc()) { if ($emails = $email->getBcc()) {
$message['Bcc'] = $this->formatAddresses($emails); $message['Bcc'] = $this->formatAddresses($emails);
} }
if ($emails = $email->getReplyTo()) {
if (1 < $length = \count($emails)) {
throw new TransportException(sprintf('Mailjet\'s API only supports one Reply-To email, %d given.', $length));
}
$message['ReplyTo'] = $this->formatAddress($emails[0]);
}
if ($email->getTextBody()) { if ($email->getTextBody()) {
$message['TextPart'] = $email->getTextBody(); $message['TextPart'] = $email->getTextBody();
} }