How to use MultiEmail Function in Laravel ?

IN this tutorial im going to learn how to send multi email in laravel using mailtrap. Follow this tutorial its define in very easy way.

First of install the project in your xampp/htdocs

composer create-project laravel/laravel multi-mail

After install go inside the project directory and open terminal and create one controller as define below.

php artisan make:controller MultiemailController

Copy below code and paste in your MultiemailController.php

<?php
namespace App\Http\Controllers;
use App\Mail\Multimail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
class MultiemailController extends Controller
{
    public function index()
    {
        return view('multiemail');
    }
    public function send(Request $request)
    {
     $this->validate($request, [
      'email'  =>  'required',
      'message' =>  'required',
     ]);
     $admin_email = $request->email;
     $multimail = (explode(",",$admin_email));
     for($count = 0; $count < count($multimail); $count++)
     {
      $data[] = array(
            'message' =>   $request->message,
            'email'   =>  $multimail[$count] );  }
    foreach($data as $d){
    $demail = $d['email'];
    Mail::to($demail)->send(new Multimail($d));
    }
    return redirect('multiemail')->with('success', 'Mail has been sent successfully!');
}
}

Next to run below command

php artisan make:mail Multimail

Next to create view file inside this directory Resources/views/multiemail.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Multi Mail using laravel 5.8 | Amit</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-2"></div>
        <div class="col-8">
            @if(session()->get('success'))
            <div class="alert alert-success mt-2">
                <button type="button" class="close" data-dismiss="alert">×</button>
                {{ session()->get('success') }}
            </div>
        @endif
            <div class="card">
                <div class="card-header">
                Multi Email Send in Laravel
                </div>
                <div class="card-body">
                    <form method="post" action="{{ route('mailsend')}}" enctype="multipart/form-data">
                        @csrf
                        <label for="">Email id</label>
                        <input type="email" class="form-control" placeholder="Enter email id" name="email" value="{{ old('email')}}">
                        <label>Enter Your Message</label>
                        <textarea type="text" name="message" class="w-100 p-2" placeholder="Enter your Message" value="{{old('message')}}"></textarea>
                        <button type="submit" name="send" class="btn btn-primary fa fa-send-o"> Submit </button>
                    </form>
                </div>
              </div>
        </div>
        <div class="col-2"></div>
    </div>
</div>
</body>
</html>

Next got to your route/web.php and paste below code

Route::get('/multiemail', 'MultiemailController@index')->name('multiemail');
Route::post('/multiemail/send', 'MultiemailController@send')->name('mailsend');

Next go to  App\Mail\Multimail.php

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Multimail extends Mailable
{
    use Queueable, SerializesModels;
    public $d;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($d) {
        $this->d = $d; }
    public function build()
    {
        return $this->from('info@testing.com')
        ->subject('New Message from anyone')
        ->view('multi_email_template')
        ->with('d', $this->d);
    }
}

Next to create one blade page

multi_email_template.blade.php

And add below code as mentioned

<p>Your Message {{ $d['message'] }}.</p>
<p>Your email Id {{ $d['email'] }}.</p>

Next add to mail trap credentials

Next go to .env and add mailtrap credentials.

Next go to your terminal and paste below code

php artisan serve 

And run below code

http://127.0.0.1:8000/multiemail

Now email sent successfully lets got to check mailtrap once email came or not.

Thanks 👍👍

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

Could not fetch https://api.github.com/repos/Setasign/FPDI/zipball/

When I composer update in Laravel its throwing error could not fetch issues so let’s start to finish this issues. 1st step go to GitHub setting section….

Error: error:0308010C:digital envelope routines::unsupported

In this tutorial I’m going to share how to solve Error: error:0308010C:digital envelope routines::unsupported. Just copy below code and run your terminal. After run above all code…

API calls from the server require an appsecret_proof argument

In this tutorial im going to solve this issue API calls from the server require an appsecret_proof argument. 1st step go to developer facebook advance setting and…

How to Delete All Records Older Than 30 Days in Laravel 10 ?

In Laravel, you can use the Eloquent ORM to delete records older than a certain number of days. Here’s an example of how you can delete all…

How to safely remove Laravel Debugbar ?

What is Laravel Debugbar ? Laravel Debugbar is a package for the Laravel PHP framework that provides a debug bar or toolbar for your application. It offers…

Example of __construct and __destruct in laravel ?

In Laravel, PHP classes can have a constructor and destructor like any other PHP class. The constructor is called when an object is created, and the destructor…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x