Send user email verification messages in Laravel

- Andrés Cruz

En español
Send user email verification messages in Laravel

Confirmation messages are emails that are sent to a client when completing an action to confirm the operation carried out, for example, changing a password, recovering a username, purchasing a product, etc., this in any web system that includes some interaction with the user is a fundamental task, therefore, we will learn to send emails from Laravel.

In Laravel, we have access to a function that we can use to confirm a user's registration.

Send confirmation messages programmatically

From a user instance, we have access to a function that sends the confirmation email to verify a user by email:

$user->sendEmailVerificationNotification();

Of course, for this you must have configured your SMTP server to send emails.

And remember to implement in your user model the classe MustVerifyEmail:

use Illuminate\Contracts\Auth\MustVerifyEmail;
***
class User extends Authenticatable implements MustVerifyEmail

Now with this, you can make use of the previous function; when using it, you will receive an email like the following:

Confirmar email

Extra: Rest Api for users to verify

Here I leave you a common use of this function on a Rest Api to register users and verify them; we have an exclusive function to send user verification emails:

class UserController extends Controller
{
    /**
     * Register
     */
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), StoreUser::myRules());
        if ($validator->fails())
            return $this->errorResponse($validator->errors(), 422);
        try {
            $user = new User();
            $user->name = $request->name;
            $user->email = $request->email;
            $user->password = Hash::make($request->password);
            $user->save();
            if ($request->subscribed) {
                Subscribe::create(['email' => $request->email]);
            }
            $success = true;
            $message = 'User register successfully';
        } catch (\Illuminate\Database\QueryException $ex) {
            $success = false;
            $message = $ex->getMessage();
        }
        // response
        $response = [
            'success' => $success,
            'message' => $message,
        ];
        $credentials = [
            'email' => $request->email,
            'password' => $request->password,
        ];
        Auth::attempt($credentials);
        $user->sendEmailVerificationNotification();
        return $this->successResponse($response);
    }
    public function verifie()
    {
        $user = Auth::user() ?? auth('sanctum')->user();
        $userModel = User::find($user->id);
        $userModel->sendEmailVerificationNotification();
        return $this->successResponse("ok");
    }

As you can see, we have a registration function, in which we obtain the user's data and once registered, we start the session with sendEmailVerificationNotification and send the email with the function explained in this post.

And a check function for you to run programmatically; In this example, we are using a Rest Api in Laravel with Laravel Sanctum with tokens, but you can use any type to verify user accounts.

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.