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
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-2-1024x510.png)
After install go inside the project directory and open terminal and create one controller as define below.
php artisan make:controller MultiemailController
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-3-1024x107.png)
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
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-4-1024x102.png)
Next to create view file inside this directory Resources/views/multiemail.blade.php
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-5-1024x414.png)
<!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
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-6-1024x464.png)
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>
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-7-1024x344.png)
Next add to mail trap credentials
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-8-1024x418.png)
Next go to .env and add mailtrap credentials.
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-9-1024x412.png)
Next go to your terminal and paste below code
php artisan serve
And run below code
http://127.0.0.1:8000/multiemail
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-10-1024x346.png)
Now email sent successfully lets got to check mailtrap once email came or not.
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-11-1024x369.png)
![](https://www.devopsfreelancer.com/blog/wp-content/uploads/2023/08/image-12-1024x419.png)
Thanks ππ
[…] How to use MultiEmail Function in Laravel ? […]