Notify admin of new user registration

This commit is contained in:
Nabeel Shahzad 2017-12-30 20:39:17 -06:00
parent d943d9bbff
commit 778780f3f2
4 changed files with 55 additions and 6 deletions

View File

@ -2,24 +2,27 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\UserStateChanged;
use Log; use Log;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
use App\Models\Enums\PilotState; use App\Models\Enums\PilotState;
use \App\Events\UserRegistered;
/** /**
* Handle sending emails on different events * Handle sending emails on different events
* @package App\Listeners * @package App\Listeners
*/ */
class EmailEventListener class NotificationEventListener
{ {
public function subscribe($events) public function subscribe($events)
{ {
$events->listen( $events->listen(
UserRegistered::class, \App\Events\UserRegistered::class,
'App\Listeners\EmailEventListener@onUserRegister' 'App\Listeners\NotificationEventListener@onUserRegister'
);
$events->listen(
\App\Events\UserStateChanged::class,
'App\Listeners\NotificationEventListener@onUserStateChange'
); );
} }
@ -34,6 +37,14 @@ class EmailEventListener
. PilotState::label($event->user->state) . PilotState::label($event->user->state)
. ', sending active email'); . ', sending active email');
# First send the admin a notification
$admin_email = setting('general.admin_email');
if (!empty($admin_email)) {
$email = new \App\Mail\Admin\UserRegistered($event->user);
Mail::to($admin_email)->send($email);
}
# Then notify the user
if($event->user->state === PilotState::ACTIVE) { if($event->user->state === PilotState::ACTIVE) {
$email = new \App\Mail\UserRegistered( $email = new \App\Mail\UserRegistered(
$event->user, $event->user,

View File

@ -0,0 +1,29 @@
<?php
namespace App\Mail\Admin;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
class UserRegistered extends Mailable
{
use Queueable, SerializesModels;
public $subject, $user;
public function __construct(User $user, $subject=null)
{
$this->subject = $subject ?: 'A new user registered';
$this->user = $user;
}
public function build()
{
return $this->markdown('emails.admin.registered')
->subject($this->subject)
->with(['user' => $this->user]);
}
}

View File

@ -19,7 +19,7 @@ class EventServiceProvider extends ServiceProvider
]; ];
protected $subscribe = [ protected $subscribe = [
'App\Listeners\EmailEventListener', 'App\Listeners\NotificationEventListener',
]; ];
/** /**

View File

@ -0,0 +1,9 @@
@component('mail::message')
A new user has signed up!
Name: {!! $user->name !!}!
Email: {!! $user->email !!}
State: {!! PilotState::label($user->state) !!}
{{ config('app.name') }}
@endcomponent