Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6dc2b06

Browse files
committed
feature #44137 [Mailer] [Mailgun] Allow multiple TagHeaders with MailgunApiTransport (starred-gijs)
This PR was merged into the 6.1 branch. Discussion ---------- [Mailer] [Mailgun] Allow multiple TagHeaders with MailgunApiTransport | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Mailgun allows you to set [3 tags per message](https://documentation.mailgun.com/en/latest/user_manual.html#tagging) but the current implementation in MailgunApiTransport, any consecutive TagHeader will override the previous one. Because it uses FormDataPart to build the payload, by using a integer key, it allows to set multiple parts with the same name #38323 The MailgunHttpTransport already supports multiple TagHeaders and I added a test to prove that. Commits ------- fa65173 [Mailer][Mailgun] Allow multiple tags with MailgunApiTransport
2 parents cbcfff2 + fa65173 commit 6dc2b06

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

src/Symfony/Component/Mailer/Bridge/Mailgun/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Allow multiple `TagHeaders` with `MailgunApiTransport`
8+
49
5.2
510
---
611

src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunApiTransportTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,38 @@ public function testSend()
152152
$this->assertSame('foobar', $message->getMessageId());
153153
}
154154

155+
public function testSendWithMultipleTagHeaders()
156+
{
157+
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
158+
$content = '';
159+
while ($chunk = $options['body']()) {
160+
$content .= $chunk;
161+
}
162+
163+
$this->assertStringContainsString("Content-Disposition: form-data; name=\"o:tag\"\r\n\r\npassword-reset\r\n", $content);
164+
$this->assertStringContainsString("Content-Disposition: form-data; name=\"o:tag\"\r\n\r\nproduct-name\r\n", $content);
165+
166+
return new MockResponse(json_encode(['id' => 'foobar2']), [
167+
'http_code' => 200,
168+
]);
169+
});
170+
$transport = new MailgunApiTransport('ACCESS_KEY', 'symfony', 'us-east-1', $client);
171+
172+
$mail = new Email();
173+
$mail->subject('Hello!')
174+
->to(new Address('[email protected]', 'Saif Eddin'))
175+
->from(new Address('[email protected]', 'Fabien'))
176+
->text('Hello There!');
177+
178+
$mail->getHeaders()
179+
->add(new TagHeader('password-reset'))
180+
->add(new TagHeader('product-name'));
181+
182+
$message = $transport->send($mail);
183+
184+
$this->assertSame('foobar2', $message->getMessageId());
185+
}
186+
155187
public function testSendThrowsForErrorResponse()
156188
{
157189
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
@@ -216,6 +248,7 @@ public function testTagAndMetadataHeaders()
216248
$email->getHeaders()->addTextHeader('h:X-Mailgun-Variables', $json);
217249
$email->getHeaders()->addTextHeader('Custom-Header', 'value');
218250
$email->getHeaders()->add(new TagHeader('password-reset'));
251+
$email->getHeaders()->add(new TagHeader('product-name'));
219252
$email->getHeaders()->add(new MetadataHeader('Color', 'blue'));
220253
$email->getHeaders()->add(new MetadataHeader('Client-ID', '12345'));
221254
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]')]);
@@ -228,8 +261,10 @@ public function testTagAndMetadataHeaders()
228261
$this->assertEquals($json, $payload['h:x-mailgun-variables']);
229262
$this->assertArrayHasKey('h:custom-header', $payload);
230263
$this->assertEquals('value', $payload['h:custom-header']);
231-
$this->assertArrayHasKey('o:tag', $payload);
232-
$this->assertSame('password-reset', $payload['o:tag']);
264+
$this->assertArrayHasKey(0, $payload);
265+
$this->assertArrayHasKey(1, $payload);
266+
$this->assertSame('password-reset', $payload[0]['o:tag']);
267+
$this->assertSame('product-name', $payload[1]['o:tag']);
233268
$this->assertArrayHasKey('v:Color', $payload);
234269
$this->assertSame('blue', $payload['v:Color']);
235270
$this->assertArrayHasKey('v:Client-ID', $payload);

src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunHttpTransportTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function testTagAndMetadataHeaders()
122122
$email = new Email();
123123
$email->getHeaders()->addTextHeader('foo', 'bar');
124124
$email->getHeaders()->add(new TagHeader('password-reset'));
125+
$email->getHeaders()->add(new TagHeader('product-name'));
125126
$email->getHeaders()->add(new MetadataHeader('Color', 'blue'));
126127
$email->getHeaders()->add(new MetadataHeader('Client-ID', '12345'));
127128

@@ -130,9 +131,12 @@ public function testTagAndMetadataHeaders()
130131
$method->setAccessible(true);
131132
$method->invoke($transport, $email);
132133

133-
$this->assertCount(3, $email->getHeaders()->toArray());
134+
$this->assertCount(4, $email->getHeaders()->toArray());
134135
$this->assertSame('foo: bar', $email->getHeaders()->get('foo')->toString());
135-
$this->assertSame('X-Mailgun-Tag: password-reset', $email->getHeaders()->get('X-Mailgun-Tag')->toString());
136+
$this->assertCount(2, $email->getHeaders()->all('X-Mailgun-Tag'));
137+
$tagHeaders = iterator_to_array($email->getHeaders()->all('X-Mailgun-Tag'));
138+
$this->assertSame('X-Mailgun-Tag: password-reset', $tagHeaders[0]->toString());
139+
$this->assertSame('X-Mailgun-Tag: product-name', $tagHeaders[1]->toString());
136140
$this->assertSame('X-Mailgun-Variables: '.json_encode(['Color' => 'blue', 'Client-ID' => '12345']), $email->getHeaders()->get('X-Mailgun-Variables')->toString());
137141
}
138142
}

src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private function getPayload(Email $email, Envelope $envelope): array
123123
}
124124

125125
if ($header instanceof TagHeader) {
126-
$payload['o:tag'] = $header->getValue();
126+
$payload[] = ['o:tag' => $header->getValue()];
127127

128128
continue;
129129
}

0 commit comments

Comments
 (0)