diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 79c9cfa89538..2cfdab44ef23 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -136,6 +136,20 @@ class Mailable implements MailableContract, Renderable */ public $diskAttachments = []; + /** + * The tags for the message. + * + * @var array + */ + protected $tags = []; + + /** + * The metadata for the message. + * + * @var array + */ + protected $metadata = []; + /** * The callbacks for the message. * @@ -190,6 +204,8 @@ public function send($mailer) $this->buildFrom($message) ->buildRecipients($message) ->buildSubject($message) + ->buildTags($message) + ->buildMetadata($message) ->runCallbacks($message) ->buildAttachments($message); }); @@ -449,6 +465,40 @@ protected function buildDiskAttachments($message) } } + /** + * Add all defined tags to the message. + * + * @param \Illuminate\Mail\Message $message + * @return $this + */ + protected function buildTags($message) + { + if ($this->tags) { + foreach ($this->tags as $tag) { + $message->getHeaders()->add(new TagHeader($tag)); + } + } + + return $this; + } + + /** + * Add all defined metadata to the message. + * + * @param \Illuminate\Mail\Message $message + * @return $this + */ + protected function buildMetadata($message) + { + if ($this->metadata) { + foreach ($this->metadata as $key => $value) { + $message->getHeaders()->add(new MetadataHeader($key, $value)); + } + } + + return $this; + } + /** * Run the callbacks for the message. * @@ -884,9 +934,9 @@ public function attachData($data, $name, array $options = []) */ public function tag($value) { - return $this->withSymfonyMessage(function (Email $message) use ($value) { - $message->getHeaders()->add(new TagHeader($value)); - }); + array_push($this->tags, $value); + + return $this; } /** @@ -898,9 +948,9 @@ public function tag($value) */ public function metadata($key, $value) { - return $this->withSymfonyMessage(function (Email $message) use ($key, $value) { - $message->getHeaders()->add(new MetadataHeader($key, $value)); - }); + $this->metadata[$key] = $value; + + return $this; } /** diff --git a/src/Illuminate/Notifications/Channels/MailChannel.php b/src/Illuminate/Notifications/Channels/MailChannel.php index 77885b87a88e..594320d5b791 100644 --- a/src/Illuminate/Notifications/Channels/MailChannel.php +++ b/src/Illuminate/Notifications/Channels/MailChannel.php @@ -9,6 +9,8 @@ use Illuminate\Notifications\Notification; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Symfony\Component\Mailer\Header\MetadataHeader; +use Symfony\Component\Mailer\Header\TagHeader; class MailChannel { @@ -144,6 +146,18 @@ protected function buildMessage($mailMessage, $notifiable, $notification, $messa $mailMessage->setPriority($message->priority); } + if ($message->tags) { + foreach ($message->tags as $tag) { + $mailMessage->getHeaders()->add(new TagHeader($tag)); + } + } + + if ($message->metadata) { + foreach ($message->metadata as $key => $value) { + $mailMessage->getHeaders()->add(new MetadataHeader($key, $value)); + } + } + $this->runCallbacks($mailMessage, $message); } diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index da4a6a678627..be77c5dbc059 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -7,9 +7,6 @@ use Illuminate\Contracts\Support\Renderable; use Illuminate\Mail\Markdown; use Illuminate\Support\Traits\Conditionable; -use Symfony\Component\Mailer\Header\MetadataHeader; -use Symfony\Component\Mailer\Header\TagHeader; -use Symfony\Component\Mime\Email; class MailMessage extends SimpleMessage implements Renderable { @@ -85,6 +82,20 @@ class MailMessage extends SimpleMessage implements Renderable */ public $rawAttachments = []; + /** + * The tags for the message. + * + * @var array + */ + public $tags = []; + + /** + * The metadata for the message. + * + * @var array + */ + public $metadata = []; + /** * Priority level of the message. * @@ -264,9 +275,9 @@ public function attachData($data, $name, array $options = []) */ public function tag($value) { - return $this->withSymfonyMessage(function (Email $message) use ($value) { - $message->getHeaders()->add(new TagHeader($value)); - }); + array_push($this->tags, $value); + + return $this; } /** @@ -278,9 +289,9 @@ public function tag($value) */ public function metadata($key, $value) { - return $this->withSymfonyMessage(function (Email $message) use ($key, $value) { - $message->getHeaders()->add(new MetadataHeader($key, $value)); - }); + $this->metadata[$key] = $value; + + return $this; } /** diff --git a/tests/Mail/MailMailableTest.php b/tests/Mail/MailMailableTest.php index 746e2c0893a3..9ed3e1d83553 100644 --- a/tests/Mail/MailMailableTest.php +++ b/tests/Mail/MailMailableTest.php @@ -445,6 +445,48 @@ public function testMailablePriorityGetsSent() $this->assertSame('hello@laravel.com', $sentMessage->getEnvelope()->getRecipients()[0]->getAddress()); $this->assertStringContainsString('X-Priority: 1 (Highest)', $sentMessage->toString()); } + + public function testMailableMetadataGetsSent() + { + $view = m::mock(Factory::class); + + $mailer = new Mailer('array', $view, new ArrayTransport); + + $mailable = new WelcomeMailableStub; + $mailable->to('hello@laravel.com'); + $mailable->from('taylor@laravel.com'); + $mailable->html('test content'); + + $mailable->metadata('origin', 'test-suite'); + $mailable->metadata('user_id', 1); + + $sentMessage = $mailer->send($mailable); + + $this->assertSame('hello@laravel.com', $sentMessage->getEnvelope()->getRecipients()[0]->getAddress()); + $this->assertStringContainsString('X-Metadata-origin: test-suite', $sentMessage->toString()); + $this->assertStringContainsString('X-Metadata-user_id: 1', $sentMessage->toString()); + } + + public function testMailableTagGetsSent() + { + $view = m::mock(Factory::class); + + $mailer = new Mailer('array', $view, new ArrayTransport); + + $mailable = new WelcomeMailableStub; + $mailable->to('hello@laravel.com'); + $mailable->from('taylor@laravel.com'); + $mailable->html('test content'); + + $mailable->tag('test'); + $mailable->tag('foo'); + + $sentMessage = $mailer->send($mailable); + + $this->assertSame('hello@laravel.com', $sentMessage->getEnvelope()->getRecipients()[0]->getAddress()); + $this->assertStringContainsString('X-Tag: test', $sentMessage->toString()); + $this->assertStringContainsString('X-Tag: foo', $sentMessage->toString()); + } } class WelcomeMailableStub extends Mailable diff --git a/tests/Notifications/NotificationMailMessageTest.php b/tests/Notifications/NotificationMailMessageTest.php index 94db5b6d284c..74631eb9f8b7 100644 --- a/tests/Notifications/NotificationMailMessageTest.php +++ b/tests/Notifications/NotificationMailMessageTest.php @@ -121,6 +121,26 @@ public function testReplyToIsSetCorrectly() $this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->replyTo); } + public function testMetadataIsSetCorrectly() + { + $message = new MailMessage; + $message->metadata('origin', 'test-suite'); + $message->metadata('user_id', 1); + + $this->assertArrayHasKey('origin', $message->metadata); + $this->assertSame('test-suite', $message->metadata['origin']); + $this->assertArrayHasKey('user_id', $message->metadata); + $this->assertSame(1, $message->metadata['user_id']); + } + + public function testTagIsSetCorrectly() + { + $message = new MailMessage; + $message->tag('test'); + + $this->assertContains('test', $message->tags); + } + public function testCallbackIsSetCorrectly() { $callback = function () {