Site Admin
Site Admin Founder of MeaningArticles
947 Views

Laravel 8 Send Email With File Attachment

Hello Dev.
nowadays i can defined to the way to send mail with file attachment in laravel 8. the way to send email with file attachment in laravel eight is so easy to use.so you can simply comply with my step by step and discover ways to send mail with file attachment in laravel 8.

we can use how to send attachment in mail the usage of laravel.you could see the way 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 upload file as attechment with sending mail in just comply with for a my few step to create a sending mail with attechment.

So let's start to the example

Step 1: Install Laravel 8

first of all we want to get clean and new Laravel eight version application the use of bellow command, now open your terminal OR command prompt and then fire bellow command and run it:

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: .Env

subsequent step to your project .env file to add the mail configration in your project and configure your project.
.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"


Step 3: Add Route

In this is step we want 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 wherein you may register web routes for your application. these
| routes are loaded with the aid of the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something tremendous!|
*/
Route::get('send-email-file-attecment', [SendMailController::class, 'index']);

 

Step 4: Add Controller

Next step to you can create a new SendMailController in your current laravel 8 project.

So let's put bellow code in SendMailController.php file.

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"] = "[email protected]";
        $data["title"] = "From meaningarticles.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 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>meaningarticles.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

You check the app on http://localhost:8000/send-email-file-attecment
i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.