Laravel 10 How To Send Email To Multiple Users Tutorial

Share this Article
Reading Time: 6 minutes
142 Views

Inside this article we will see the concept i.e Laravel 10 How To Send Email To Multiple Users Tutorial. Article contains the classified information i.e How to send bulk email to users in Laravel.

Tutorial is a step-by-step guide on how to send email to multiple recipients using the Laravel framework. It covers the basics of setting up and configuring the email system in Laravel, creating a Mailable class, and sending bulk emails to multiple users.

By following this tutorial, developers can learn how to implement email marketing strategies in their Laravel projects and improve user engagement.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

How To Configure SMTP in Laravel?

To configure SMTP details, open up the file .env from application root.

We will pass mail driver as gmail server, mail host, mail port, mail username, mail password.

//..

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME="xxxyyyzzz@gmail.com"
MAIL_PASSWORD="your_password"
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="xxxyyyzzz@gmail.com"
MAIL_FROM_NAME="Online Web Tutor"

//...

Make sure you have enabled google security setting form your gmail. go to Google account and click on “Account”. Once you are on the “Account” page, click on “Security“. Scroll down to the bottom and you will find “Less secure app access” settings. Set as ON.

Have a look into these images.

Create Mailable Class in Laravel

We will create mail class via artisan command which will be responsible for sending emails.

$ php artisan make:mail MyTestMail

Application will create a folder with the name of Mail inside /app folder. You should see a file MyTestMail.php inside /app/Mail folder.

Open file MyTestMail.php and write this code into it.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;

    public $details;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this->subject('Mail from Online Web Tutor')
            ->view('emails.sample-mail');

        return $this;
    }
}

Email Template Setup

Let’s create a folder emails inside /resources/views folder. Inside this folder we will create a simple blade template file with following content.

Create a fie sample-mail.blade.php inside /resources/views/emails folder.

Open file and write this code into it.

<!DOCTYPE html>
<html>

<head>
    <title>Test Mail From Online Web Tutor</title>
</head>

<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
    <p>Thank you <br />Online Web Tutor</p>
</body>

</html>

Add Route

Open web.php file from /routes folder. Add this route and code into it.

//...
use Illuminate\Support\Facades\Mail;

//...

Route::get('send-mail', function () {

    // Email data details
    $details = [
        'title' => 'Email to Multiple Users',
        'body' => 'This is sample content we have added for this test mail sending to multiple users.'
    ];

    // Email to users
    $users = [
        "xxxxxx@gmail.com",
        "xxxxxxx@gmail.com"
    ];

    foreach ($users as $user) { // sending mail to users.

        Mail::to($user)->send(new \App\Mail\MyTestMail($details));
    }

    dd("Email is Sent, please check your inbox.");
});

//...

Concept

This is the concept by which we will send email to multiple users.

// Email to users
$users = [
    "xxxxxx@gmail.com",
    "xxxxxxx@gmail.com"
];

foreach ($users as $user) { // sending mail to users.

    Mail::to($user)->send(new \App\Mail\MyTestMail($details));
}

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1/send-mail

Each user will get this email.

We hope this article helped you to learn about Laravel 10 How To Send Email To Multiple Users Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.