From 4d7facdcb2a3ce9e56efb7d016eb058226dec31f Mon Sep 17 00:00:00 2001 From: Nicolas Dousson Date: Wed, 22 Feb 2023 11:43:08 +0100 Subject: [PATCH] Add new attributes to Infobip API transport to support reporting behavior --- .../Mailer/Bridge/Infobip/CHANGELOG.md | 5 ++ .../Transport/InfobipApiTransportTest.php | 70 +++++++++++++++++++ .../Infobip/Transport/InfobipApiTransport.php | 13 ++++ 3 files changed, 88 insertions(+) diff --git a/src/Symfony/Component/Mailer/Bridge/Infobip/CHANGELOG.md b/src/Symfony/Component/Mailer/Bridge/Infobip/CHANGELOG.md index 7174cd7fdeedd..1320fd5f75bc2 100644 --- a/src/Symfony/Component/Mailer/Bridge/Infobip/CHANGELOG.md +++ b/src/Symfony/Component/Mailer/Bridge/Infobip/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add reporting behavior thanks to new attributes support + 6.2 --- diff --git a/src/Symfony/Component/Mailer/Bridge/Infobip/Tests/Transport/InfobipApiTransportTest.php b/src/Symfony/Component/Mailer/Bridge/Infobip/Tests/Transport/InfobipApiTransportTest.php index 020d897559fd6..86470e1591905 100644 --- a/src/Symfony/Component/Mailer/Bridge/Infobip/Tests/Transport/InfobipApiTransportTest.php +++ b/src/Symfony/Component/Mailer/Bridge/Infobip/Tests/Transport/InfobipApiTransportTest.php @@ -241,6 +241,51 @@ public function testSendEmailWithAttachmentsShouldCalledInfobipWithTheRightParam ); } + public function testSendEmailWithHeadersShouldCalledInfobipWithTheRightParameters() + { + $email = $this->basicValidEmail(); + $email->getHeaders() + ->addTextHeader('X-Infobip-IntermediateReport', 'true') + ->addTextHeader('X-Infobip-NotifyUrl', 'https://foo.bar') + ->addTextHeader('X-Infobip-NotifyContentType', 'application/json') + ->addTextHeader('X-Infobip-MessageId', 'RANDOM-CUSTOM-ID'); + + $this->transport->send($email); + + $options = $this->response->getRequestOptions(); + $this->arrayHasKey('body'); + $this->assertStringMatchesFormat(<<<'TXT' + %a + --%s + Content-Type: text/plain; charset=utf-8 + Content-Transfer-Encoding: 8bit + Content-Disposition: form-data; name="intermediateReport" + + true + --%s + Content-Type: text/plain; charset=utf-8 + Content-Transfer-Encoding: 8bit + Content-Disposition: form-data; name="notifyUrl" + + https://foo.bar + --%s + Content-Type: text/plain; charset=utf-8 + Content-Transfer-Encoding: 8bit + Content-Disposition: form-data; name="notifyContentType" + + application/json + --%s + Content-Type: text/plain; charset=utf-8 + Content-Transfer-Encoding: 8bit + Content-Disposition: form-data; name="messageId" + + RANDOM-CUSTOM-ID + --%s-- + TXT, + $options['body'] + ); + } + public function testSendMinimalEmailWithSuccess() { $email = (new Email()) @@ -357,6 +402,31 @@ public function testSendEmailWithAttachmentsWithSuccess() ); } + public function testSendEmailWithHeadersWithSuccess() + { + $email = $this->basicValidEmail(); + $email->getHeaders() + ->addTextHeader('X-Infobip-IntermediateReport', 'true') + ->addTextHeader('X-Infobip-NotifyUrl', 'https://foo.bar') + ->addTextHeader('X-Infobip-NotifyContentType', 'application/json') + ->addTextHeader('X-Infobip-MessageId', 'RANDOM-CUSTOM-ID'); + + $sentMessage = $this->transport->send($email); + + $this->assertInstanceOf(SentMessage::class, $sentMessage); + $this->assertStringMatchesFormat( + <<<'TXT' + %a + X-Infobip-IntermediateReport: true + X-Infobip-NotifyUrl: https://foo.bar + X-Infobip-NotifyContentType: application/json + X-Infobip-MessageId: RANDOM-CUSTOM-ID + %a + TXT, + $sentMessage->toString() + ); + } + public function testSentMessageShouldCaptureInfobipMessageId() { $this->response = new MockResponse('{"messages": [{"messageId": "somexternalMessageId0"}]}'); diff --git a/src/Symfony/Component/Mailer/Bridge/Infobip/Transport/InfobipApiTransport.php b/src/Symfony/Component/Mailer/Bridge/Infobip/Transport/InfobipApiTransport.php index ad20f468417c2..7902b9cdff250 100644 --- a/src/Symfony/Component/Mailer/Bridge/Infobip/Transport/InfobipApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Infobip/Transport/InfobipApiTransport.php @@ -33,6 +33,13 @@ final class InfobipApiTransport extends AbstractApiTransport { private const API_VERSION = '3'; + private const HEADER_TO_MESSAGE = [ + 'X-Infobip-IntermediateReport' => 'intermediateReport', + 'X-Infobip-NotifyUrl' => 'notifyUrl', + 'X-Infobip-NotifyContentType' => 'notifyContentType', + 'X-Infobip-MessageId' => 'messageId', + ]; + private string $key; public function __construct(string $key, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null) @@ -123,6 +130,12 @@ private function formDataPart(Email $email, Envelope $envelope): FormDataPart $this->attachmentsFormData($fields, $email); + foreach ($email->getHeaders()->all() as $header) { + if ($convertConf = self::HEADER_TO_MESSAGE[$header->getName()] ?? false) { + $fields[$convertConf] = $header->getBodyAsString(); + } + } + return new FormDataPart($fields); }