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

Asked

Viewed 1,821 times

2

I need that when a user is registered he takes the value of the email field and passes to the function $message->to($request->email) to send the email to the registered user. See how I am doing. Only in the object $message->to() not being dynamically. How do I be dynamic with the data coming from 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 that in this object $Massage->to (here had to be dynamic) Kind of $message->to($request->email) //email coming from the form. How could I do this?

1 answer

6


For this purpose you can use Laravel’s own triggers, I’m 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 Observers allows you to fire 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 a:

php artisan make:notification NotificarNovoUsuario

This will generate the file App Notifications Notificarnovousuario.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 [
            //
        ];
    }
}

All I did was modify the method toMail to insert my own message.

And finally you edit your App Providers Appserviceprovider.php and changes the method boot for:

public function boot()
{
    User::observe(ObservarUser::class);
}
  • 1

    Nice guy that you taught me. I’m very grateful. Thank you very much.

  • More where this conf. . config('app.name') ?

  • Inside the file. env you have a line with the name of your app, I do not remember the name of the head parameter, but I think it is the first line, this conf(? app.name') can be overwritten there. Or even inside the config/app.php file

  • 1

    Well I took a look and that’s right. vlw Gave it right for what I want. Obg even

  • And you live next door in Brasilia , I’m here from Luziania. Vlw

  • Ball show then. Anything we are around. I climbed a project in Laravel on my Github if you want to check: github.com/fabiojaniolima/Webupload

  • add to share some ideas. fb.com/Natan.support

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.