Skip to content

Email Notifications

Rick Mann edited this page Jun 3, 2016 · 7 revisions

Email notifications

I found very easy to send normal notification and email notifications in one go.

Create a sendWithEmail() custom method, like following:

use Fenos\Notifynder\Contracts\Sender;
use Fenos\Notifynder\Contracts\NotifynderSender;
use Fenos\Notifynder\Models\Notification;
use Mail;

class EmailNotificationSender implements Sender
{
    /**
     * @var array
     */
    protected $notification;

    /**
     * Create a new email notification sender instance.
     *
     * @param  array  $notification
     */
    public function __construct($notification)
    {
        $this->notification = $notification;
    }

    /**
     * Send the notification.
     *
     * @param  NotifynderSender  $sender
     * @return void
     */
    public function send(NotifynderSender $sender)
    {
        $notification = $sender->sendOne($this->notification);
        $this->sendEmail($notification);
    }

    /**
     * Send an email notification.
     *
     * @param  Notification  $notification
     * @return void
     */
    public function sendEmail(Notification $notification) {
        $user = $notification->to;

        Mail::send(
            'emails.notification',
            compact('user', 'notification'),
            function ($message) use ($user) {
                return $message->to($user->email)->subject('Notification');
            }
        );
    }
}

Let's register the custom sender we just created

app/Providers/NotificationServiceProvider.php

public function boot() {
    Notifynder::extend('sendWithEmail', function($notification) {
         return new EmailNotificationSender($notification);
    });
}

Now we are ready to notify our users with a internal notification and an email that will be delivered into their inbox.

Usage example sending a notification to the currently authenticated user:

Notifynder::category('sayhello')
    ->from($someUser)
    ->to(Auth::user())
    ->url('http://localhost')
    ->extra(compact('period_day'))
    ->sendWithEmail();
Clone this wiki locally