10-Apr-2023
.
Admin
Hi guys,
Today i will today i will explained to the how to send email with file attachment in laravel- 8 project.How to send email with file attachment in laravel- 8 is so easy to use.So you can just follow my step by step and learn how to send email with file attachment in laravel- 8.
We will use how to send attachment in mail using laravel.You can see how to attach file in mail in laravel and implement a send attachment in mail in laravel.
We can send email with attachment in laravel 6, laravel 7 and laravel 8 application. and send email with add file as attechment with sending mail in just follow for a my few step to create a sending mail with attechment.
So let's start to the example.
Step 1: Install a New Project
First step to create a new project in just following command through.
composer create-project --prefer-dist laravel/laravel blog
Step 2 :.Env
Next step to your project .env file to add the mail configration in your project and configure your project.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
mail_username=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
mail_from_address=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Step 3: Add Route
In this is step we need to create routes for items listing. so open your "routes/web.php" file and add following route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendMailController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('send-email-file-attecment', [SendMailController::class, 'index']);
Step 4: Add new SendMailController
Next step to you can create a new SendMailController in your current laravel 8 project.
So let's put bellow code.
app/Http/Controllers/SendMailController.php
<?php
namespace App\Http\Controllers;
use PDF;
use Mail;
class SendMailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$data["email"] = "xyz@gmail.com";
$data["title"] = "From Nicesnippest.com";
$data["body"] = "This is Demo Mail Attechment Pdf File";
$attechfiles = [
public_path('file/test1.pdf'),
public_path('file/test2.pdf'),
];
Mail::send('emails.fileAttechmemtMail', $data, function($message)use($data, $attechfiles) {
$message->to($data["email"], $data["email"])
->subject($data["title"]);
foreach ($attechfiles as $file){
$message->attach($file);
}
});
dd('Mail sent successfully Check Send Mail Email Address.');
}
}
Step 5: Add View File
Finally last step to you can create a blade file in fileAttechmemtMail.blade.php(resources/views/emails/fileAttechmemtMail.blade.php) for a layout file and use the code.
resources/views/emails/fileAttechmemtMail.blade.php
<!DOCTYPE html<
<html>
<head>
<title>Nicesnippets.com</title>
</head>
<body>
<h1>File Attechment Mail,</h1>
<p>This Is File Attechment Mail Example,</p>
<p>Thank You.</p>
</body>
</html>
So, finally now you can run the code in check the example
php artisan serve
http://localhost:8000/send-email-file-attecment
It will help you...
Output
#Laravel 8
#Laravel 7
#Laravel
#Laravel 6