Laravel 7 Tutorial: Attaching PDFs to Your Emails


How to Send Mail with PDF attachment in Laravel 7

In this article we will learn how to send Mail with PDF attachment of your View file in Laravel . Sending PDF as an attachment is a very easy process but you need to understand well . So let's see how to send mail with PDF attachment with example .

For this tutorial i will be using for Sending mail and DOMPdf Package for PDF generation .

Table of Content :

  • Setting up Mail Configuration
  • Install DOMPdf
  • Setup Route
  • Controller setup
  • Check For Result

Step 1 - Setup Mail Configuration :

The very first step you need to do is to setup your Mail Configuration in your project . I am using Mailtrap for sending mail , you can refer my regarding how to setup Mailrtrap For your Laravel Project .

Projectname/.env :


After configuring your " .env " do not forget to restart your local server otherwise it might not work .

Step 2 - Install DOMPdf :

Use the following command to install DOMPdf package . You can also refer the official github site for installing DOMPdf .

composer require barryvdh/laravel-dompdf

Add the following Service Provider on your Providers array inside " config/app.php " .

config/app.php :

'providers' => [


Add the following class on the aliases array inside " config/app.php "

'aliases' => [
'PDF' => Barryvdh\DomPDF\Facade::class,


Now your DOMPdf setup is done . Now we need to setup our routes and controllers for sending mail with PDF attachment .

Step 3 - Setup Route :

Setup your Route and controller as shown in following -

routes/web.php :


Step 4 : Controller Setup :

app/Http/Controllers/MailController.php :


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
    public function sendmail()
            $data["subject"]='sending pdf as attachment';

            //Generating PDF with all the post details

            $pdf = PDF::loadView('userdata');  // this view file will be converted to PDF

            //Sending Mail to the corresponding user

            Mail::send([], $data, function($message)use($data,$pdf) {
            $message->to($data["email"], $data["client_name"])
            ->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
                       'mime' => 'application/pdf',
            ->setBody('Hi, welcome user! this is the body of the mail');


PDF::loadview('userdata') basically used to make the " userdata.blade.php " view file into PDF .

Note :-

Here inside the first parameter of " send() " method , i am passing a blank array but you can also pass a view file also for the Mail body ( e.g :- Mail::send('viewfile' , ... , ...) ) . But the important thing is you cannot provide the view file name which is provided in the ' PDF::loadview('viewfile') ' method .

resources/views/userdata.blade.php :

<!DOCTYPE html>
<h3>this file is the content of your PDF</h3>
 Your content here

That's it . Just hit your route and the the mail will be send with pdf attachment .

Output :

Thank you for reading this article 😊

For any query do not hesitate to comment 💬

Post a Comment

Post a Comment