How to Send Email with Attachment in Laravel 8

How to Send Email with Attachment in Laravel 8

In this example you learn how to send email with attachment in laravel 8 project.  you can send email with attachment file in laravel 8. The attach() method to you can simply attach file and send mail.

We will use how to send attachment in mail using laravel. In this example we discuss on laravel 8 send mail with attach file. You can see how to attach file in mail in laravel and implement a send attachment in mail in laravel.

Read Also :  Laravel Where Not in Eloquent Example

After read this post you simply send email with attachment in laravel 6, laravel 7 and laravel 8 application.

Laravel 8 Send Email With File Attachment

Step 1: Install Laravel

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

Step 2 : Change.Env

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

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

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

Step 4: Add new SendMailController

<?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>Web-tuts.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-attacment

I hope you understand of laravel send email with attachment and it can help you…