How To Send Email With Attachment In Laravel 8

Websolutionstuff | May-05-2021 | Categories : Laravel PHP

In this tutorial i will show you how to send email with attachment in laravel 8. As we all know mail functionalities are common in all project but if you want to send mail with attechment then this post is for you here we will see how to attach file in mail in laravel 8.

Here,we will send attachment in mail in laravel 8. So, Let's start and follow below steps.

Step 1 : Set Configuration for send email

Step 2 : Add Route

Step 3 : Add Controller

Step 4 : Create Blade File

 

Step 1 : Set Configuration for send email

First of all we need to add send email configuration with mail driver, mail host, mail port, mail username, mail password etc in your .env file

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=Your user name
MAIL_PASSWORD=Your pwd
MAIL_ENCRYPTION=TLS

 

Step 2 : Add Route

Now, add route in your routes/web.php file.

Route::get('send/mail', [SendMailController::class, 'send_mail'])->name('send_mail');

 

Step 3 : Add Controller

Now create SendMailController in your project and copy below code for attach file in mail.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;

class SendMailController extends Controller
{
    public function send_mail(Request $request)
    {
        $data["email"] = "[email protected]";
        $data["title"] = "websolutionstuff.com";
        $data["body"] = "This is test mail with attachment";
 
        $files = [
            public_path('attachments/Image_1.jpeg'),
            public_path('attachments/Laravel_8_pdf_Example.pdf'),
        ];
  
        Mail::send('mail.Test_mail', $data, function($message)use($data, $files) {
            $message->to($data["email"])
                    ->subject($data["title"]);
 
            foreach ($files as $file){
                $message->attach($file);
            }            
        });

        echo "Mail send successfully !!";
    }
}

 

Step 4 : Create Blade File

Now, create blade file in this file location resources\views\mail\Test_mail.blade.php for display messages in mail

Hi, Websolutionstuff <br/>
This is Test Mail.<br />
Thank you...!!

 

And finally you will get output like below image.

how_to_send_email_with_attachment_in_laravel_8_output

 

Recommended Post
Featured Post
Laravel 9 orWhere Condition Example
Laravel 9 orWhere Condition Ex...

In this article, we will see the laravel 9 orWhere condition example. Where condition is joined together using the ...

Read More

Oct-13-2022

Carbon Add Days To Date In Laravel 9
Carbon Add Days To Date In Lar...

In this article, we will see carbon add day and add days to date in laravel 9. Carbon provides the addDay() and add...

Read More

Nov-17-2022

Laravel 9 Cron Job Task Scheduling Tutorial
Laravel 9 Cron Job Task Schedu...

In this article, we will see laravel 9 cron job task scheduling tutorial, many times we require to run some piece o...

Read More

Mar-17-2022

How To Create Dependent Dropdown In Laravel
How To Create Dependent Dropdo...

In this article, we will see how to create a dependent dropdown list in laravel using ajax. Many times we have requ...

Read More

Jul-05-2020