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 Mailtrap.io 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 :


MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=34d7af640c9818
MAIL_PASSWORD=d07708a6993e38
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=studywithkishan@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

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' => [
-----------------
-----------------
-----------------
Barryvdh\DomPDF\ServiceProvider::class,

],


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 :


Route::get('/sendpdf','MailController@sendmail');


Step 4 : Controller Setup :

app/Http/Controllers/MailController.php :


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
    public function sendmail()
    {
      
         
         
            $data["email"]='useremail@gmail.com';
            $data["client_name"]='user_name';
            $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"])
            ->subject($data["subject"])
            ->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>
<html>
<head>
 <title>PDF</title>
</head>
<body>
<h3>this file is the content of your PDF</h3>
<p>
 Your content here
</p>
</body>
</html>

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 💬



1 Comments

Previous Post Next Post

Contact Form