Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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