Laravel 5.x send reset password email to all users in bulk

By | October 19, 2019

Hello, Scientists, This article is about Laravel 5.x send reset password email to all users in bulk manually. We will need this functionality when we are transferring website users to the laravel website.

For example, if we have a website made in WordPress and now a new website builds in Laravel so before to make it live we need to send emails to all users to reset there password as WordPress and Laravel encryption are different.

If you have the same scenario then you are at the right place, please follow the below tutorial to make it happen!

I hope you have installed Laravel already in your PC. (if not then follow this article Install Laravel)

Do require settings in .env file

Database setting as below

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel-reset-password-bulk
DB_USERNAME=root
DB_PASSWORD=pass123

SMTP setting as below (** Here I used mailtrap you can use your own SMTP)

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=75df91facfc5d8
MAIL_PASSWORD=108574c16e047c
MAIL_ENCRYPTION=null

Install Laravel Auth if not installed yet

$ composer require laravel/ui --dev
$ php artisan ui vue --auth

In my case, I am using default js and CSS without a view so I am adding js and CSS manually because this article is not about vue specific. now run below command to migrate the database.

$ php artisan migrate

Create controller `SendresetemailsController` using artisan

$ php artisan make:controller SendresetemailsController

Create one method in that controller with below code which will fetch all users records from the database and send them reset password email.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Mail;
use Illuminate\Support\Facades\Lang;

class SendresetemailsController extends Controller
{
    
    /**
     * Send emails to all users at once when you moved all users to other website to here so they can change password by link
     * @$hok : 19/Oct/2019
     */
    public function index(Request $request){

        $users = User::all();

        $sentEmails = [];
        foreach($users as $k=>$v){
            $broker = Password::broker();

            $sentEmails[] = $v->email;

            $user = User::where("email", $v->email)->first();

            $reset_token = $broker->createToken($user); // flat token

            $reset_link = url(config('app.url').route('password.reset', ['token' => $reset_token, 'email' => $user->email], false));

            $result = Mail::send('emails.password_reset_custom', [
                'fullname' => $user->name,
                'reset_url' => $reset_link,
                'line1' => 'You are receiving this email because we received a password reset request for your account.',
                'line2' => 'This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.users.expire')],
                'line3' => 'If you did not request a password reset, no further action is required.',
                'line4' => 'If you’re having trouble clicking the "Reset Password" button, copy and paste the URL below into your web browser:',
                'copyright' => '© '.date('Y').' '.env('APP_NAME').'. All rights reserved.'
            ], function($message) use ($user){
                $message->subject('Reset Password Notification');
                $message->to($user->email);
            });           

            //Remove below sleep() when you have proper smtp or adjust it based on your need
            sleep(2); // in cash time out issue
        }  

        echo "<pre>";
        echo "<h2>Reset password email sent to below users</h2>";
        print_r($sentEmails);
        exit;      
    }
}

Please make sure to use the required namespace.

Create one view [views/emails] password_reset_custom.blade.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta content="width=device-width, initial-scale=1.0" name="viewport">
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
            </meta>
        </meta>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; background-color: #f8fafc; color: #74787e; height: 100%; hyphens: auto; line-height: 1.4; margin: 0; -moz-hyphens: auto; -ms-word-break: break-all; width: 100% !important; -webkit-hyphens: auto; -webkit-text-size-adjust: none; word-break: break-word;">
        <style>
            @media  only screen and (max-width: 600px) {
            .inner-body {
                width: 100% !important;
            }

            .footer {
                width: 100% !important;
            }
        }

        @media  only screen and (max-width: 500px) {
            .button {
                width: 100% !important;
            }
        }
        </style>
        <table cellpadding="0" cellspacing="0" class="wrapper" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; background-color: #f8fafc; margin: 0; padding: 0; width: 100%; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%;" width="100%">
            <tr>
                <td align="center" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;">
                    <table cellpadding="0" cellspacing="0" class="content" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; margin: 0; padding: 0; width: 100%; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%;" width="100%">
                        <tr>
                            <td class="header" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; padding: 25px 0; text-align: center;">
                                <a href="{{ env('APP_URL') }}" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #bbbfc3; font-size: 19px; font-weight: bold; text-decoration: none; text-shadow: 0 1px 0 white;">
                                    {{ env('APP_NAME') }}
                                </a>
                            </td>
                        </tr>
                        <!-- Email Body -->
                        <tr>
                            <td cellpadding="0" cellspacing="0" class="body" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; background-color: #ffffff; border-bottom: 1px solid #edeff2; border-top: 1px solid #edeff2; margin: 0; padding: 0; width: 100%; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%;" width="100%">
                                <table align="center" cellpadding="0" cellspacing="0" class="inner-body" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; background-color: #ffffff; margin: 0 auto; padding: 0; width: 570px; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 570px;" width="570">
                                    <!-- Body content -->
                                    <tr>
                                        <td class="content-cell" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; padding: 35px;">
                                            <h1 style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #3d4852; font-size: 19px; font-weight: bold; margin-top: 0; text-align: left;">
                                                Hello! {{ $fullname }},
                                            </h1>
                                            <p style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #3d4852; font-size: 16px; line-height: 1.5em; margin-top: 0; text-align: left;">
                                                {{ $line1 }}
                                            </p>
                                            <table align="center" cellpadding="0" cellspacing="0" class="action" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; margin: 30px auto; padding: 0; text-align: center; width: 100%; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%;" width="100%">
                                                <tr>
                                                    <td align="center" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;">
                                                        <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;" width="100%">
                                                            <tr>
                                                                <td align="center" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;">
                                                                    <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;">
                                                                        <tr>
                                                                            <td style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;">
                                                                                <a class="button button-primary" href="{{ $reset_url }}" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; border-radius: 3px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); color: #fff; display: inline-block; text-decoration: none; -webkit-text-size-adjust: none; background-color: #3490dc; border-top: 10px solid #3490dc; border-right: 18px solid #3490dc; border-bottom: 10px solid #3490dc; border-left: 18px solid #3490dc;" target="_blank">
                                                                                    Reset Password
                                                                                </a>
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </table>
                                            <p style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #3d4852; font-size: 16px; line-height: 1.5em; margin-top: 0; text-align: left;">
                                                {{ $line2 }}
                                            </p>
                                            <p style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #3d4852; font-size: 16px; line-height: 1.5em; margin-top: 0; text-align: left;">
                                                {{ $line3 }}
                                            </p>
                                            <p style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #3d4852; font-size: 16px; line-height: 1.5em; margin-top: 0; text-align: left;">
                                                Regards,
                                                <br>
                                                    {{ env('APP_NAME') }}
                                                </br>
                                            </p>
                                            <table cellpadding="0" cellspacing="0" class="subcopy" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; border-top: 1px solid #edeff2; margin-top: 25px; padding-top: 25px;" width="100%">
                                                <tr>
                                                    <td style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;">
                                                        <p style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #3d4852; line-height: 1.5em; margin-top: 0; text-align: left; font-size: 12px;">
                                                            {{ $line4 }}
                                                            <a href="{{ $reset_url }}" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; color: #3869d4;">
                                                                {{ $reset_url }}
                                                            </a>
                                                        </p>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box;">
                                <table align="center" cellpadding="0" cellspacing="0" class="footer" role="presentation" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; margin: 0 auto; padding: 0; text-align: center; width: 570px; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 570px;" width="570">
                                    <tr>
                                        <td align="center" class="content-cell" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; padding: 35px;">
                                            <p style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; box-sizing: border-box; line-height: 1.5em; margin-top: 0; color: #aeaeae; font-size: 12px; text-align: center;">
                                                {{ $copyright }}
                                                
                                            </p>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>

You can edit and add your HTML stuff inside the above view file and content.

Add route in routes/web.php

Route::any('send-reset-password-in-bulk', 'SendresetemailsController@index')->name('send-reset-password-in-bulk');

While in this demo I used MailTrap so it will restrict too many emails at the same time (so I have added sleep time inside) but if you will have proper SMTP then it will run smoothly.

Now its time to run demo.

$ php artisan serve

Add some users in the database using the register page for testing. as below you can see users in the database in the user’s tables.

Reset password in bulk manually - users database

After serving the artisan you can run below URL to send email’s now

http://127.0.0.1:8000/send-reset-password-in-bulk

You can see below the response of success notification

success-with-response

To double confirm please check in your sent email of the SMTP server. as I used mailtrap you can see I got all the emails successfully.

So this article is about Laravel 5.x send reset password email to all users in bulk manually. Its all done. if you guys find any difficulties we are happy to help. keep us posted!

Below is the GITHUB repository for this integration.

Leave a Reply

Your email address will not be published. Required fields are marked *