Spread the love

In this article i will show you to Send Mail in Laravel to multiple recipients, without view and without SMTP. We can send our mail to multiple recipients using the same to method with array collection with email and name. if you do not want to use SMTP then you can configure for sendmail.

Email is very common operation of any website like sending an email to users after registration, Send newsletters to users and many more. Laravel provides multiple drivers or services to send mail from different different providers.

Here is the basic example

 foreach (['taylor@example.com', 'dries@example.com'] as $recipient) {
     \Mail::to($recipient)
            ->cc($moreUsers)
            ->bcc($evenMoreUsers)
            ->send(new \App\Mail\TestMail($user));
   } 

Laravel gives wide range of options to send emails. We can queue an email for send on specific time using the queue drivers.

Following are the supported methods of Laravel mail

  1. Amazon SES
  2. MailGun
  3. Sendmail
  4. SparkPost
  5. SMTP

Laravel Mailing supports blade views based templating system so you can easily configure the mail body layout same as other pages.

It also gives a easy to implements mail with attachment feature so let’s start with example

Step 1: Create a laravel project

First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel example-app

Step 2 : Configure the mail configuration

We have two options to configure the mail from .env file or config/mail.php file and the recommended way to configure it is from .enc file

So for SendMail

MAIL_DRIVER=sendmail 
MAIL_HOST=localhost
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 3 : Generate Mailable Class

In laravel 8 or 9 every mail is implements mailable class thus first we need to create a mail class as follow

php artisan make:mail TestMail

This will create a class in following location App\Mail\TestMail.php

<?php

namespace App\Mail;

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

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('test');
    }
}

Update the code as below

<?php

namespace App\Mail;

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

class TestMail extends Mailable
{
    use Queueable, SerializesModels;
    private $user;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user=$user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('info@readerstacks.com')
               ->subject('Mail from readerstacks.com')
               ->view('emails.test',["user"=>$this->user,"title"=>"Register"]);
    }
}

Step 4 : Create View for Mail

Create a simple view for mail body in resources/views/emails/test.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Readerstacks.com</title>
</head>
<body>
    <h1>Hello {{ $user['name'] }}, download the attachement</h1>
    
    <p>{{ $user['body'] }}</p>
    <p>Thanks to visit us.</p>
    
</body>
</html>

Step 5 : Create Route and Send Mail

Now, create a route and call Mail methods in inline function as below

<?php
use Illuminate\Support\Facades\Route;


Route::get('/send-mail',function(){
 $user = [
        'name' => 'Readerstacks',
        'body' => 'This is simple mail from Readerstacks'
    ];
   foreach (['taylor@example.com', 'dries@example.com'] as $recipient) {
     \Mail::to($recipient)
            ->cc($moreUsers)
            ->bcc($evenMoreUsers)
            ->send(new \App\Mail\TestMail($user));
   } 
  
   
   
});

Also Read : How to Send Mail with Attachment(PDF, docs,image) in Laravel ?

Leave a Reply