Skip to content

Commit 7c90894

Browse files
[12.x] Add mergeMetadata method to the Mailable class (#56376)
* Add mergeMetadata method to the Mailable class. * Update the test case for the mergeMetadata method. * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 89207fb commit 7c90894

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,13 +1184,17 @@ public function hasTag($value)
11841184
/**
11851185
* Add a metadata header to the message when supported by the underlying transport.
11861186
*
1187-
* @param string $key
1188-
* @param string $value
1187+
* @param array|string $key
1188+
* @param string|null $value
11891189
* @return $this
11901190
*/
1191-
public function metadata($key, $value)
1191+
public function metadata($key, $value = null)
11921192
{
1193-
$this->metadata[$key] = $value;
1193+
if (is_array($key)) {
1194+
$this->metadata = array_merge($this->metadata, $key);
1195+
} else {
1196+
$this->metadata[$key] = $value;
1197+
}
11941198

11951199
return $this;
11961200
}

tests/Mail/MailMailableTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,45 @@ public function testMailableMetadataGetsSent()
634634
}
635635
}
636636

637+
public function testMailableMergeMetadata()
638+
{
639+
$mailable = new WelcomeMailableStub;
640+
$mailable->to('hello@laravel.com');
641+
$mailable->from('taylor@laravel.com');
642+
$mailable->html('test content');
643+
644+
$mailable->metadata([
645+
'template_id' => 'external-template-id',
646+
'customer_id' => 101,
647+
'order_id' => 1000,
648+
'subtotal' => 1500,
649+
'gst' => 150,
650+
'shipping_fee' => 20,
651+
'total' => 1670,
652+
]);
653+
654+
$this->assertTrue($mailable->hasMetadata('template_id', 'external-template-id'));
655+
$this->assertTrue($mailable->hasMetadata('customer_id', 101));
656+
$this->assertTrue($mailable->hasMetadata('order_id', 1000));
657+
$this->assertTrue($mailable->hasMetadata('subtotal', 1500));
658+
$this->assertTrue($mailable->hasMetadata('gst', 150));
659+
$this->assertTrue($mailable->hasMetadata('shipping_fee', 20));
660+
$this->assertTrue($mailable->hasMetadata('total', 1670));
661+
662+
$this->stubMailer();
663+
$view = m::mock(Factory::class);
664+
$mailer = new Mailer('array', $view, new ArrayTransport);
665+
666+
$sentMessage = $mailer->send($mailable);
667+
$this->assertStringContainsString('X-Metadata-template_id: external-template-id', $sentMessage->toString());
668+
$this->assertStringContainsString('X-Metadata-customer_id: 101', $sentMessage->toString());
669+
$this->assertStringContainsString('X-Metadata-order_id: 1000', $sentMessage->toString());
670+
$this->assertStringContainsString('X-Metadata-subtotal: 1500', $sentMessage->toString());
671+
$this->assertStringContainsString('X-Metadata-gst: 150', $sentMessage->toString());
672+
$this->assertStringContainsString('X-Metadata-shipping_fee: 20', $sentMessage->toString());
673+
$this->assertStringContainsString('X-Metadata-total: 1670', $sentMessage->toString());
674+
}
675+
637676
public function testMailableTagGetsSent()
638677
{
639678
$this->stubMailer();

0 commit comments

Comments
 (0)