Join WhatsApp ChannelJoin Now

Laravel 8 Send Email With File Attachment

Hi Dev,

Today, i we will show you laravel 8 send email with file attachment. This article will give you simple example of laravel 8 send email with file attachment. you will laravel 8 send email with file attachment. In this article, we will implement a laravel 8 send email with file attachment.

We will use the way to send attachment in mail victimisation laravel.You can see the way to attach get in mail in laravel and implement a send attachment in mail in laravel.

We can send email with attachment in laravel vi, laravel seven and laravel eight application. and send email with add file as attechment with causation mail in exactly follow for a my few step to form a causation mail with attechment.

So let’s follow few step to create example of laravel 8 send email with file attachment.

Step 1: Install a New Laravel

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

Step 2: .Env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
mail_username=google@gmail.com
MAIL_PASSWORD=123456789
MAIL_ENCRYPTION=tls
mail_from_address=google@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 3: Add Route

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendMailController; 

Route::get('send-email-file-attecment', [SendMailController::class, 'index']);  

Step 4: Add new SendMailController

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

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> 

Run

php artisan serve

I hope it will assist you…

Recommended Posts