Skip to content

[Mailer] Add new attributes to Infobip API transport #49491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Infobip/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add reporting behavior thanks to new attributes support

6.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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"}]}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down