For this purpose you can use Laravel's own triggers, I am referring to Observers . One of the advantages is that you are already using something ready, which is part of the core of Laravel. Not to mention of course that Observers allows you to trigger automatic email in other situations as well.
First we create the file: App \ Observers \ ObservarUser.php with the following content:
<?php
namespace App\Observers;
use App\User;
use App\Notifications\NotificarNovoUsuario;
class ObservarUser
{
/**
* Listen to the User created event.
*
* @param User $user
* @return void
*/
public function created(User $user)
{
$user->notify(new NotificarNovoUsuario());
}
/**
* Listen to the User deleting event.
*
* @param User $user
* @return void
*/
public function deleting(User $user)
{
//
}
}
Note that in addition to created you have other possible methods that can be used following the same logic.
And now we've created one:
php artisan make:notification NotificarNovoUsuario
This will generate the file App \ Notifications \ NotifyNewUsername.php with the following content:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class NotificarNovoUsuario extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Seja bem-vindo ao ' . config('app.name'))
->line('É com grande alegria que te damos as boas vindas!')
->action('Clique aqui para acessar ', url('/'))
->line('Qualquer dúvida nossa equipe está sempre a disposição!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
The only thing I did was modify the toMail method to insert my own message.
And finally you edit your App \ Providers \ AppServiceProvider.php and change the boot method to:
public function boot()
{
User::observe(ObservarUser::class);
}