Skip to content

Instantly share code, notes, and snippets.

@paulund
Last active March 23, 2019 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulund/1a0c862229c1dea9a305150cfc776d61 to your computer and use it in GitHub Desktop.
Save paulund/1a0c862229c1dea9a305150cfc776d61 to your computer and use it in GitHub Desktop.
How to send an email to admin when new users sign up.
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class AdminNewUser extends Mailable
{
use Queueable, SerializesModels;
/**
* @var User
*/
private $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('New User')
->markdown('mail.new-user', [
'user' => $this->user
]);
}
}
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Registered;
use App\Mail\AdminNewUser;
use Illuminate\Support\Facades\Mail;
class AdminNewUserListener
{
/**
* Handle the event.
*
* @param Registered $event
* @return void
*/
public function handle(Registered $event)
{
Mail::to(config('mail.from.address'))->send(new AdminNewUser($event->user));
}
}
<?php
namespace App\Providers;
use App\Listeners\NewUser;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Auth\Events\Registered;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
NewUser::class
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
@component('mail::message')
# New User
A new user with email {{ $user->email }} has registered to your site.
@endcomponent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment