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

[5.3] Ability to set from address in mail notifications #15055

Merged
merged 1 commit into from
Aug 26, 2016
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
4 changes: 4 additions & 0 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function send($notifiable, Notification $notification)
$m->to($recipients);
}

if (! empty($message->from)) {
$m->from($message->from[0], isset($message->from[1]) ? $message->from[1] : null);
}

$m->subject($message->subject ?: Str::title(
Str::snake(class_basename($notification), ' ')
));
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class MailMessage extends SimpleMessage
*/
public $view = 'notifications::email';

/**
* The from address for the message.
*
* @var array
*/
public $from = [];

/**
* The view data for the message.
*
Expand Down Expand Up @@ -47,6 +54,19 @@ public function view($view, array $data = [])
return $this;
}

/**
* Set the from address for the mail message.
*
* @param string $address
* @return $this
*/
public function from($address, $name = null)
{
$this->from = [$address, $name];

return $this;
}

/**
* Attach a file to the message.
*
Expand Down
180 changes: 180 additions & 0 deletions tests/Notifications/NotificationMailChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,149 @@ public function testMailIsSentByChannel()

$channel->send($notifiable, $notification);
}

public function testMessageWithSubject()
{
$notification = new NotificationMailChannelTestNotification;
$notifiable = new NotificationMailChannelTestNotifiable;

$message = $notification->toMail($notifiable);
$data = $message->toArray();

$channel = new Illuminate\Notifications\Channels\MailChannel(
$mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class)
);

$mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) {
$mock = Mockery::mock('Illuminate\Mailer\Message');

$mock->shouldReceive('subject')->once()->with('test subject');

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

$mock->shouldReceive('from')->never();

$closure($mock);

return true;
}));

$channel->send($notifiable, $notification);
}

public function testMessageWithoutSubjectAutogeneratesSubjectFromClassName()
{
$notification = new NotificationMailChannelTestNotificationNoSubject;
$notifiable = new NotificationMailChannelTestNotifiable;

$message = $notification->toMail($notifiable);
$data = $message->toArray();

$channel = new Illuminate\Notifications\Channels\MailChannel(
$mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class)
);

$mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) {
$mock = Mockery::mock('Illuminate\Mailer\Message');

$mock->shouldReceive('subject')->once()->with('Notification Mail Channel Test Notification No Subject');

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

$closure($mock);

return true;
}));

$channel->send($notifiable, $notification);
}

public function testMessageWithMultipleSenders()
{
$notification = new NotificationMailChannelTestNotification;
$notifiable = new NotificationMailChannelTestNotifiableMultipleEmails;

$message = $notification->toMail($notifiable);
$data = $message->toArray();

$channel = new Illuminate\Notifications\Channels\MailChannel(
$mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class)
);

$mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) {
$mock = Mockery::mock('Illuminate\Mailer\Message');

$mock->shouldReceive('subject')->once();

$mock->shouldReceive('to')->never();

$mock->shouldReceive('bcc')->with(['[email protected]', '[email protected]']);

$closure($mock);

return true;
}));

$channel->send($notifiable, $notification);
}

public function testMessageWithFromAddress()
{
$notification = new NotificationMailChannelTestNotificationWithFromAddress;
$notifiable = new NotificationMailChannelTestNotifiable;

$message = $notification->toMail($notifiable);
$data = $message->toArray();

$channel = new Illuminate\Notifications\Channels\MailChannel(
$mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class)
);

$mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) {
$mock = Mockery::mock('Illuminate\Mailer\Message');

$mock->shouldReceive('subject')->once();

$mock->shouldReceive('to')->once();

$mock->shouldReceive('from')->with('[email protected]', 'Test Man');

$closure($mock);

return true;
}));

$channel->send($notifiable, $notification);
}

public function testMessageWithFromAddressAndNoName()
{
$notification = new NotificationMailChannelTestNotificationWithFromAddressNoName;
$notifiable = new NotificationMailChannelTestNotifiable;

$message = $notification->toMail($notifiable);
$data = $message->toArray();

$channel = new Illuminate\Notifications\Channels\MailChannel(
$mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class)
);

$mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) {
$mock = Mockery::mock('Illuminate\Mailer\Message');

$mock->shouldReceive('subject')->once();

$mock->shouldReceive('to')->once();

$mock->shouldReceive('from')->with('[email protected]', null);

$closure($mock);

return true;
}));

$channel->send($notifiable, $notification);
}
}

class NotificationMailChannelTestNotifiable
Expand All @@ -35,10 +178,47 @@ class NotificationMailChannelTestNotifiable
public $email = '[email protected]';
}

class NotificationMailChannelTestNotifiableMultipleEmails
{
use Illuminate\Notifications\Notifiable;

public function routeNotificationForMail()
{
return ['[email protected]', '[email protected]'];
}
}

class NotificationMailChannelTestNotification extends Notification
{
public function toMail($notifiable)
{
return (new MailMessage)
->subject('test subject');
}
}

class NotificationMailChannelTestNotificationNoSubject extends Notification
{
public function toMail($notifiable)
{
return new MailMessage;
}
}

class NotificationMailChannelTestNotificationWithFromAddress extends Notification
{
public function toMail($notifiable)
{
return (new MailMessage)
->from('[email protected]', 'Test Man');
}
}

class NotificationMailChannelTestNotificationWithFromAddressNoName extends Notification
{
public function toMail($notifiable)
{
return (new MailMessage)
->from('[email protected]');
}
}