How to send email to multiple recipients in laravel?

How to send email to multiple recipients in laravel?

Hello Artisans.

In this article, i will show you a complete guide on how to send emails to multiple recipients. This post goes into details on how to email send to multiple recipients in laravel. I will give you complete guide on how to send emails from php Laravel to multiple recipients. We will use laravel 8 to send multiple emails. You can use this example for laravel 6, laravel 7, laravel 8 and laravel 9.

So, let's get started.

Step 1: Install Laravel

If you have already a laravel setup then this is not required just skip this step.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Mail Configuration

You must have to configure a sender mail configuration to send emails from your larave application. In this example, i'm using Sendinblue email service. So, let's add send in blue mail configuration in laravel .env file. You can check here complete guide on how to setup SendInBlue account and send single email.

...
MAIL_MAILER=smtp
MAIL_HOST=smtp-relay.sendinblue.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
...

Read also: How to send email with Gmail SMTP in Laravel?

Step 3: Create Mail Class

Create a mail class WelcomeMail to send email to all users. Just run the below command to create mail class. 

php artisan make:mail WelcomeMail

It will create a file WelcomeMail.php in app/Mail directory.

app/Mail/WelcomeMail.php

<?php

namespace App\Mail;

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

class WelcomeMail 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()
    {
        return $this->subject('Welcome to Coders Vibe')
        ->view('mails.welcome_mail');
    }
}

Step 4: Create WelcomeMail view file

Now, let's create a welcome email blade file. I have created a file welcome_mail.blade.php in /resources/views/mails/ directory.

resources/views/mails/welcome_mail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Codersvibe.com</title>
</head>
<body>

	<h1>Hi, {{ $user->name }}</h1>
    <h4>Welcome to Coders Vibe</h4>
</body>
</html>

Step 5: Create route in route/web.php file

Now we need to create a route to trigger our mailable class to send the email to multiple users at the same time. 

route/web.php

...
Route::get('send-mail-to-users', [UserController::class,'sendmails']);

Step 6: Create controller

We have to create a new UserController. In this file, we will handle the mail requests to send emails to multiple recipients. So, let's add code to send emails in this controller. 

We have different methods to send same email to multiple recipients in laravel.

we can use foreach loop to send multiple emails. We have to send email to all users or active users, so we need to create a foreach loop for users to send emails in loop. 

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use App\Mail\UserEmail;
use Mail;
  
class UserController extends Controller
{
    /**
     * Send the email to multiple recipients with foreach loop
     *
     * @return response()
     */
    public function sendEmail(Request $request)
    {
        $users = User::all();
  
        foreach ($users as $key => $user) {
            Mail::to($user->email)->send(new UserEmail($user));
        }
  
        return response()->json(['success'=>'Send email successfully.']);
    }
}

We can add an array to send multiple emails to different recipients. It's a good way to send emails to multiple recipients without foreach loop in laravel. 

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use App\Mail\UserEmail;
use Mail;
  
class UserController extends Controller
{
    /**
     * Send the email to multiple recipients with BCC method
     *
     * @return response()
     */
    public function sendEmail(Request $request)
    {
    	$users = User::all();
            
        Mail::to($users)->send(new UserEmail($user));
  
        return response()->json(['success'=>'Send email successfully.']);
    }
}

Step 7: Sending Mail

Now you can send multiple emails by calling your route. 

https://127.0.0.1:8000/send-mail-to-users

I hope, it will help you. 

Happy Coding. 

Leave a Comment

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

Go To Top
×