How to send multiple emails in Laravel?
How to send multiple emails in Laravel?

In this part of the Laravel tutorial, we will teach you how to send multiple emails in Laravel. In this section, we will learn how to send multiple emails at once with the help of Laravel Mail class. previously, we have introduced Laravel Mail Class and the drivers that Laravel provides for sending emails in the post "How to send emails in Laravel?"; So if you have not read this post yet, you can refer to it first.

In this tutorial, we will use Gmail SMTP to send emails. This allows us to easily send emails on localhost. By following the steps below you will be able to easily send multiple emails simultaneously using Laravel.

  • Step 1: Prepare the env file

First, we will introduce the needed driver and our Gmail account in the env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail@gmail.com
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_gmail@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
  • Step 2: Create an email class

We will create the MyTestMail.php class in app> Mail by executing the following command in the terminal.

php artisan make:mail MyTestMail

You will see that the corresponding class will be created with the following commands.

app/Mail/MyTestMail.php

<?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()
    {
        return $this->subject('Mail from ItSolutionStuff.com')
                    ->view('emails.myTestMail');
    }
}
  • Step 3: Create a Blade View

We will create the myTestMail.blade.php view in resources> views> emails and put the following commands in it.

resources/views/emails/myTestMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>www.maryam-hajireza.ir</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
   
    <p>Thank you</p>
</body>
</html>
  • Step 4: Create a Route

In this section, we will create a web route to send the emails.

routes/web.php

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

    $emails = array("myemail1@email.com", "myemail2@email.com");

    $details = [
        'title' => 'Mail from www.maryam-hajireza.ir',
        'body' => 'This is for testing email using smtp'
    ];
   
    \Mail::send('emails.myTestMail', $details, function($message) use ($emails)
    {
        $message->bcc($emails)->subject('Message from www.maryam-hajireza.ir');
    });
   
    dd("Email is Sent.");
});

Execute the project by running the following command in the terminal.

php artisan serve

Go to the path below and see that the emails will be sent correctly.

http://localhost:8000/send-mail