Skip to content

Commit

Permalink
[9.x] Fix mails with tags and metadata are not queuable (#41028)
Browse files Browse the repository at this point in the history
* fix: mails with tags and metadata could not be queued

* test: add test cases for tags en metadata for mail

* fix: mail could have multiple tags

* style: fix issues from style linter

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
joostdebruijn and taylorotwell authored Feb 15, 2022
1 parent 8e15796 commit 8ac224f
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 15 deletions.
62 changes: 56 additions & 6 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -190,6 +204,8 @@ public function send($mailer)
$this->buildFrom($message)
->buildRecipients($message)
->buildSubject($message)
->buildTags($message)
->buildMetadata($message)
->runCallbacks($message)
->buildAttachments($message);
});
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

Expand Down
29 changes: 20 additions & 9 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Mail/MailMailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,48 @@ public function testMailablePriorityGetsSent()
$this->assertSame('[email protected]', $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('[email protected]');
$mailable->from('[email protected]');
$mailable->html('test content');

$mailable->metadata('origin', 'test-suite');
$mailable->metadata('user_id', 1);

$sentMessage = $mailer->send($mailable);

$this->assertSame('[email protected]', $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('[email protected]');
$mailable->from('[email protected]');
$mailable->html('test content');

$mailable->tag('test');
$mailable->tag('foo');

$sentMessage = $mailer->send($mailable);

$this->assertSame('[email protected]', $sentMessage->getEnvelope()->getRecipients()[0]->getAddress());
$this->assertStringContainsString('X-Tag: test', $sentMessage->toString());
$this->assertStringContainsString('X-Tag: foo', $sentMessage->toString());
}
}

class WelcomeMailableStub extends Mailable
Expand Down
20 changes: 20 additions & 0 deletions tests/Notifications/NotificationMailMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ public function testReplyToIsSetCorrectly()
$this->assertSame([['[email protected]', null], ['[email protected]', '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 () {
Expand Down

0 comments on commit 8ac224f

Please sign in to comment.