Skip to content

Instantly share code, notes, and snippets.

@abenevaut
Last active November 30, 2017 19:46
Show Gist options
  • Save abenevaut/6b30eded0029c5f1e930e02d7e76ff25 to your computer and use it in GitHub Desktop.
Save abenevaut/6b30eded0029c5f1e930e02d7e76ff25 to your computer and use it in GitHub Desktop.
A Laravel 5 channel to send Mailable notifications to administrator
<?php
namespace abenevaut\App\Notifications\Channels;
use Illuminate\Notifications\Notification;
class AdministratorMailableChannel implements MailableChannelInterface
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return void
*/
public function send($notifiable, Notification $notification)
{
$notification
->toAdministrator($notifiable)
->to(config('mail.from.address'), config('mail.from.name'))
->from(config('mail.from.address'), config('mail.from.name'))
->send(app('mailer'));
}
}
<?php
namespace abenevaut\App\Notifications\Messages;
use Illuminate\Notifications\Messages\MailMessage;
class CustomerMailMessage extends MailMessage
{
/**
* CustomerMailMessage constructor.
*/
public function __construct()
{
$this->from(config('mail.from.address'), config('mail.from.name'));
}
}
/**
* Send the handshake confirmation email.
*
* @param $subject
* @param $body
*
* @return $this
*/
public function sendHandshakeMailToConfirmReceptionToSenderNotification($subject, $body)
{
$this->notify(new HandshakeMailToConfirmReceptionToSender($this, $subject, $body));
return $this;
}
<?php
namespace abenevaut\App\Notifications\Users\Users;
use abenevaut\Infrastructure\Interfaces\Queues\ShouldQueueInterface;
use abenevaut\Infrastructure\Contracts\Queues\QueueableTrait;
use abenevaut\Infrastructure\Contracts\Notifications\Notification;
use abenevaut\Infrastructure\Interfaces\Domain\Users\Users\HandshakableInterface;
use abenevaut\App\Notifications\Messages\CustomerMailMessage;
use abenevaut\App\Notifications\Channels\AdministratorMailableChannel;
use abenevaut\App\Notifications\Messages\MailableMessage;
class HandshakeMailToConfirmReceptionToSender extends Notification
{
/**
* @var HandshakableInterface|null
*/
protected $entity = null;
/**
* @var string
*/
protected $subject = '';
/**
* @var string
*/
protected $body = '';
/**
* HandshakeMailToConfirmedReceptionToSender constructor.
* @param HandshakableInterface $entity
*/
public function __construct(HandshakableInterface $entity, $subject, $body)
{
$this->entity = $entity;
$this->subject = $subject;
$this->body = $body;
}
/**
* Get the notification's channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return ['mail', AdministratorMailableChannel::class];
}
/**
* Build the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new CustomerMailMessage)
->subject(trans('mails.handshake_subject', ['subject' => $this->subject]))
->view(
'emails.users.users.handshake_mail_to_confirme_reception_to_sender',
[
'civility_name' => $this->entity->civility_name,
'body' => $this->body,
]
);
}
/**
* @param $notifiable
*
* @return MailableMessage
*/
public function toAdministrator($notifiable)
{
return (new MailableMessage())
->subject(trans('mails.handshake_subject', ['subject' => $this->subject]))
->view(
'emails.users.users.handshake_mail_to_administrator',
[
'civility_name' => $this->entity->civility_name,
'body' => $this->body,
]
);
}
}
<?php
namespace abenevaut\App\Notifications\Channels;
use Illuminate\Notifications\Notification;
interface MailableChannelInterface
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*/
public function send($notifiable, Notification $notification);
}
<?php
namespace abenevaut\App\Notifications\Messages;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MailableMessage extends Mailable
{
use Queueable, SerializesModels;
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment