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

How to Crawl any website Meta Title and Meta Description in Laravel ?

1st step install below package. Next to create Controller First go to route and put below code Next go to controller and put below code Next go…

SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into `oauth_clients` (`user_id`, `name`, `secret`

In this tutorial i’m going to solve the error SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into oauth_clients (user_id, name, secret Error :-…

Top 20 Laravel Interview Question in 2024

In this tutorial im going share interview experience For laravel developer. A list of top frequently asked Laravel Interview Questions and answers are given below. Q #1) What is…

How to Get Google Analytics API key ?

In this tutorial we’re going to share how to get the google Analytics API key. I have shared in very easy way. First go enable Google analytics…

Youtube Subscriber Count in ReactJs

In this tutorial i’m going to learn how to count YouTube Subsribers count, views count as well, as define below. In order to install your app, first…

How to Disable Laravel’s Eloquent timestamps in laravel ?

In this tutorial we’re going to share how to disable the automatic created_at and updated_at timestamps in Laravel’s Eloquent models, along with explanations of different scenarios: 1st…

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] How to use MultiEmail Function in Laravel ? […]

1
0
Would love your thoughts, please comment.x
()
x