How to Send PDF Attachment using Email in Laravel 10

Last Updated on by in Laravel

This comprehensive tutorial will teach you how to send emails with PDF attachments in Laravel using the laravel PDF package.

PDF stands for Portable Document Format. It is a popular file format created by Adobe.

The primary vision of the creator is to provide users with a facile yet reliable way to present and exchange documents.

PDF offers the best way to exchange information between mails.

If you are a laravel developer willing to know how to build a controller that helps you send PDF attachments with mail in Laravel, then this guide is for you.

We need a couple of things to send pdf attachments with email. You will need a laravel app, a mail trap for sending and testing emails with a pdf attachment, a controller, routes, and a view file.

Laravel 10 Send PDF Attachment with Email Example

  • Step 1: Build Laravel Project
  • Step 2: Install PDF Package
  • Step 3: Set Up Laravel PDF
  • Step 4: Register SMTP in Env
  • Step 5: Set Up Send Email Controller
  • Step 6: Create Send PDF Attachment View
  • Step 7: Build New Routes
  • Step 8: Run Application Server

Build Laravel Project

A new laravel application can be created using Composer. If you have installed the Composer, then you can run the given command to build a new project.

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

Install PDF Package

To mail PDF attachments in Laravel is made possible by the laravel-pdf package. This library does the core work for us; you have to install it to take advantage.

composer require niklasravnsborg/laravel-pdf

Set Up Laravel PDF

We have to move into the app.php file that you can see in the config/ directory.

You must register the service provider and facades in providers and aliases, respectively.

'providers' => [
  /* Application Service Providers */

'aliases' => Facade::defaultAliases()->merge([
   'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class

We are now going to run the command; our purpose is to publish the library config file to the config folder.

php artisan vendor:publish

Register SMTP in Env

In order to send a mail with pdf, you have to add the credentials inside the .env config file. You can look for this file inside your laravel project.


You have to allow non-secure apps if you are willing to use Gmail, it will enable the less secure app; make sure to visit here.

Set Up Send Email Controller

You have to run the command to generate a new controller. All the code with logic goes here into this file to build the desired feature.

php artisan make:controller PdfMailController

After invoking the above command, a new file is created; hence, you have to open the app/Http/Controllers/PdfMailController.php file further and add the given code to the file.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;

class PdfMailController extends Controller
    public function index(Request $request){
            $data["email"] = "";
            $data["title"] = "PositronX";
            $data["body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
            $files = [
            Mail::send('mail.demo_mail', $data, function($message)use($data, $files) {
                foreach ($files as $file){
            echo "Mail successfully sent!";

We are now going to create a mail template using the Laravel blade file.

Make sure to create resources\views\mail\demo_mail.blade.php file and insert the following code into the file.

Hi, Admin <br/>
This is just a demo mail.<br />
Thank you!!

Create Send PDF Attachment View

Further, you have to set up the view file. You need to get into the resources/views folder; here, you must make the home.blade.php and add the given code to the file.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel 9 Email PDF Attachment Example</title>

    <body class="antialiased">
       <h2>A demo mail sent from</h2>
       <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam non ligula ligula. </p>

Build New Routes

Go ahead and look for the resources/ folder; inside here, you will see web.php file. Make sure to define the route that will help you shoot the mail with a pdf attachment.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfMailController;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('send-pdf-via-mail', [PdfMailController::class, 'index']);

Run Application Server

We have successfully created the functionality; now you need to run the server using given command. Make sure to test the feature using provided url.

php artisan serve

How to Send PDF Attachment using Email in Laravel


Laravel PDF library is an mPDF wrapper especially built for Laravel 5 and higher. It quite easily generates PDF documents from HTML.

In this extensive post, we taught you how to use the Laravel PDF library to send PDF attachments via email.

Digamber - Author

Hi, I'm Digamber Singh, a New Delhi-based full-stack developer, tech author, and open-source contributor with 10+ years' experience in HTML, CSS, JavaScript, PHP, and WordPress.