Skip to content
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

[7.x] Add plain mail to notifications #33725

Merged
merged 3 commits into from
Aug 1, 2020
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
8 changes: 6 additions & 2 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ protected function messageBuilder($notifiable, $notification, $message)
protected function buildView($message)
{
if ($message->view) {
return $message->view;
return [
'html' => $message->view,
'text' => $message->textView,
];
}

if (property_exists($message, 'theme') && ! is_null($message->theme)) {
Expand All @@ -115,7 +118,8 @@ protected function additionalMessageData($notification)
'__laravel_notification_id' => $notification->id,
'__laravel_notification' => get_class($notification),
'__laravel_notification_queued' => in_array(
ShouldQueue::class, class_implements($notification)
ShouldQueue::class,
class_implements($notification)
),
];
}
Expand Down
27 changes: 25 additions & 2 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class MailMessage extends SimpleMessage implements Renderable
*/
public $view;

/**
* The plain text view to use for the message.
*
* @var string
*/
public $textView;

/**
* The view data for the message.
*
Expand Down Expand Up @@ -104,13 +111,28 @@ class MailMessage extends SimpleMessage implements Renderable
public function view($view, array $data = [])
{
$this->view = $view;
$this->viewData = $data;
$this->viewData = array_merge($this->viewData, $data);

$this->markdown = null;

return $this;
}

/**
* Set the plain text view for the message.
*
* @param string $textView
* @param array $data
* @return $this
*/
public function text($textView, array $data = [])
{
$this->textView = $textView;
$this->viewData = array_merge($this->viewData, $data);

return $this;
}

/**
* Set the Markdown template for the notification.
*
Expand Down Expand Up @@ -311,7 +333,8 @@ public function render()
{
if (isset($this->view)) {
return Container::getInstance()->make('mailer')->render(
$this->view, $this->data()
[$this->view, $this->textView],
$this->data()
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/Integration/Notifications/Fixtures/html.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
htmlContent
1 change: 1 addition & 0 deletions tests/Integration/Notifications/Fixtures/plain.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plainContent
50 changes: 50 additions & 0 deletions tests/Integration/Notifications/SendingMailNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Mockery as m;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -61,6 +62,8 @@ protected function getEnvironmentSetUp($app)
$app->extend(MailFactory::class, function () {
return $this->mailFactory;
});

View::addLocation(__DIR__.'/Fixtures');
}

protected function setUp(): void
Expand Down Expand Up @@ -245,6 +248,38 @@ public function testMailIsSentUsingMailable()

$user->notify($notification);
}

public function testMailIsSentUsingMailMessageWithPlain()
{
$notification = new TestMailNotificationWithPlain;
$notification->id = Str::uuid()->toString();

$user = NotifiableUser::forceCreate([
'email' => '[email protected]',
]);

$this->mailer->shouldReceive('send')->once()->with(
['html' => 'html', 'text' => 'plain'],
array_merge($notification->toMail($user)->toArray(), [
'__laravel_notification_id' => $notification->id,
'__laravel_notification' => get_class($notification),
'__laravel_notification_queued' => false,
]),
m::on(function ($closure) {
$message = m::mock(Message::class);

$message->shouldReceive('to')->once()->with(['[email protected]']);

$message->shouldReceive('subject')->once()->with('Test Mail Notification With Plain');

$closure($message);

return true;
})
);

$user->notify($notification);
}
}

class NotifiableUser extends Model
Expand Down Expand Up @@ -328,3 +363,18 @@ public function toMail($notifiable)
return $mailable;
}
}

class TestMailNotificationWithPlain extends Notification
{
public function via($notifiable)
{
return [MailChannel::class];
}

public function toMail($notifiable)
{
return (new MailMessage)
->view('html')
->text('plain');
}
}
30 changes: 30 additions & 0 deletions tests/Notifications/NotificationMailMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@ public function testTemplate()

$this->assertSame('notifications::foo', $message->markdown);
}
public function testHtmlView()
{
$message = new MailMessage;

$this->assertSame(null, $message->view);
$this->assertSame([], $message->viewData);

$message->view('notifications::foo', [
'foo' => 'bar',
]);

$this->assertSame('notifications::foo', $message->view);
$this->assertSame(['foo' => 'bar'], $message->viewData);
}

public function testPlainView()
{
$message = new MailMessage;

$this->assertSame(null, $message->textView);
$this->assertSame([], $message->viewData);

$message->text('notifications::foo', [
'foo' => 'bar',
]);

$this->assertSame('notifications::foo', $message->textView);
$this->assertSame(['foo' => 'bar'], $message->viewData);
}


public function testCcIsSetCorrectly()
{
Expand Down