Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Send Mail in Laravel 8 : 9 to multiple recipients, CC and without SMTP

How to Send Mail in Laravel 8 / 9 to multiple recipients, CC and without SMTP ?

Aman Jain, May 11, 2022May 11, 2022

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 ?

Related

Php Laravel Laravel 9 emaillaravelphpsendmailsmtp

Post navigation

Previous post
Next post

Related Posts

Javascript Laravel Customized Autocomplete JQuery UI

Laravel Customized Autocomplete JQuery UI

July 12, 2022July 12, 2022

Laravel Customized Autocomplete JQuery UI is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel to provide the data in json response. In this tutorial we…

Read More
Php How to get random rows in laravel example

How to get random rows in laravel example ?

October 10, 2022March 16, 2024

In this blog post, we’ll take a look at how to get random rows in Laravel database. We’ll explore the use of the QueryBuilder, Eloquent, and the DB facade. laravel has inbuilt eloquent method to get the random records from database using the inRandomOrder method. we will understand the random…

Read More
Php laravel belongsTo

How to Use Laravel belongsTo Relationship in with Example ?

February 27, 2022November 10, 2023

Laravel BelongsTo relationship is used to create the relation between two tables. belongsTo means create the relation one to one in inverse direction or its opposite of hasOne. For example if a user has a profile and we wanted to get profile with the user details then we can use…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version