How To Send Email With Attachment In Laravel 9

Websolutionstuff | Mar-16-2022 | Categories : Laravel PHP

In this article, we will see how to send email with attachments in laravel 9. As we all know mail functionalities are common in all project but if you want to send mail with attechment in some cases then this post is for you here we will see how to attach file in mail in laravel 9.

So, let's see send email with attachment in laravel 9, how to send attachment in mail using laravel 9, send pdf attachment in mail laravel 9, laravel 9 send email with attachment

Step 1 : Set Configuration for sending email

Step 2 : Add Route

Step 3 : Add Controller

Step 4 : Create Blade File

 

Step 1 : Set Configuration for sending 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 and copy the below code in your controller.

<?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 a blade file in this file location resources\views\mail\Test_mail.blade.php for display messages in the mail

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

Output :

how_to_send_email_with_attachment_in_laravel_9_output

 


You might also like :

Recommended Post
Featured Post
Laravel 8 Socialite Login with Google Account
Laravel 8 Socialite Login with...

In this article, we will see laravel 8 socialite login with a google account. This post gives you an example of a larave...

Read More

Dec-01-2020

Scrolla - jQuery Plugin for Reveal Animations
Scrolla - jQuery Plugin for Re...

In this tutorial we will see how to use scrolla - jQuery plugin for reveal animations when scrolling a mouse. this jquer...

Read More

Apr-21-2021

How To Change Datepicker Color In Angular 15 Material
How To Change Datepicker Color...

In this tutorial, I will guide you through the process of changing the color of a datepicker component in Angular 15 Mat...

Read More

Jul-05-2023

Integrating ChatGPT with Node and Vue
Integrating ChatGPT with Node...

In a world where technology and human interaction blend seamlessly, artificial intelligence (AI) holds incredible potent...

Read More

Jul-17-2023