How to send an email after the user registration has been made? Laravel 5.4

1

I need a user to get the value of the email field and pass it to the $message->to($request->email) function to send the email to the registered user. See how I'm doing. Except that the $message->to() object is not being dynamically. How do I get to be dynamic with the data coming from the Request?

Mail::send('email.send', ['title' => 'Cadastro de usuario de email', 'content' => 'teste teste'], function ($message)
            {

                $message->from('[email protected]', 'Natan Melo');

                $message->to('[email protected]');

            });

Only in this $ massage-> object (here it had to be dynamic) Type so $message->to($request->email) // email coming from the form. How could I do that?

    
asked by anonymous 04.10.2017 / 02:13

1 answer

3

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);
}
    
04.10.2017 / 02:39