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 ππ
[…] How to use MultiEmail Function in Laravel ? […]