Table of Contents
In this tutorial we're going to learn how we can send an email to the application admin user when a new user registers to your Laravel application.
In this example we're going to use events and listeners to know when a user has registered and the listener will then send an email to the admin of the application.
To create this boilerplate you simply use the following command.
php artisan make:auth
You will now have a new folder in your
App/Http/Controllers folder called
Auth. This will have controllers for logging in, register, forgotten password and resetting your password.
The register controller uses a trait of
RegistersUsers.php this will give you the boilerplate methods for signing up new users.
As you can see from
\Illuminate\Foundation\Auth\RegistersUsers::register it will dispatch a new
Registered event, which we can listen for in our app.
Add the following to your application event provider.
To generate new events you can add this to the
$listen property. You list out an array using the event class as the key of the array and listeners are the node vales of the array. This means that a single event can perform multiple actions by adding more listeners to the event array.
The listener will need to take the user from the event class and send an email to the global
mail.from.address. In the config folder you have a default
mail.php file this has all the default settings for the email of your app. One of these settings is the global from and name email address for your application. As this is normally the admin user so we will be using this global email address in this example.
The mailable class uses the markdown component functionality of Laravel and pass in the new user into the blade component. We can then post this information to the admin user so they know who has just signed up to the application.
That's it, that's all the code you need to send the admin an alert when a new user registers to your application.