How To Send Email With Attachment In Laravel 8

In this article, we will see how to send an email with an attachment in laravel 8. As we all know mail functionalities are common in all projects but if you want to send a mail with an attachment then this post is for you here we will see how to attach files in mail laravel 8.

So, let's see how to send attachments in the mail using laravel 8, how to attach files in the mail in laravel 8, send multiple attachments in mail laravel 8, laravel 8 send an email with PDF attachments, send multiple attachments in mail laravel 8, how to send image in the mail using laravel 8.

Step 1 : Set Configuration for Sending Email with Attachments

Step 2 : Add Routes

Step 3 : Add Controller

Step 4 : Create Blade File

 

Step 1 : Set Configuration for Sending Email with Attachments

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_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=TLS

 

 

Step 2 : Add Route

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

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

 

Step 3 : Add Controller

Now, we will create SendMailController and copy the below code for the attached file in the mail.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;

class SendMailController extends Controller
{
    public function send_mail(Request $request)
    {
        $data["email"] = "test@gmail.com";
        $data["title"] = "techsolutionstuff.com";
 
        $files = [
            public_path('attachments/Laravel.jpeg'),
            public_path('attachments/Laravel_8_mail_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 a blade file in this file location resources\views\mail\Test_mail.blade.php for display messages in the mail

Hi<br/>
This is test mail with attachments.<br />
Thank you !!

Output :

how_to_send_email_with_attachment_in_laravel_8_output

 


You might also like :

RECOMMENDED POSTS

FEATURE POSTS