Laravel 11 Generate PDF and Send Email Example

Hello, laravel web developers! In this article, we'll see how to generate a PDF file and send an email in laravel 11. In laravel 11, we'll send an email with a PDF attachment. In many cases, we are required to send emails with attached documents like PDF, Excel, and CSV for reports.

So, we'll generate a PDF file using the DomPDF composer package. After that, we'll send an email using Gmail SMTP. 

Laravel 11 Generate PDF and Send Email Example

Laravel 11 Generate PDF and Send Email Example

 

Step 1: Install Laravel 11 Application

In this step, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel example-app

 

Step 2: Install the DomPDF Package

Next, we'll install barryvdh/laravel-dompdf composer package using the following command. 

composer require barryvdh/laravel-dompdf

 

Step 3: Define Configuration

Then, we'll define the configuration for sending email using Gmail SMTP.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=test@123
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=from@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

Step 4: Create Mail Class

Next, we'll create a mailable class using the following artisan command.

php artisan make:mail PDFMailExample

app/Mail/PDFMailExample.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Attachment;
  
class PDFMailExample extends Mailable
{
    use Queueable, SerializesModels;
  
    public $mailData;
  
    /**
     * Create a new message instance.
     */
    public function __construct($mailData)
    {
        $this->mailData = $mailData;
    }
  
    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: $this->mailData['title'],
        );
    }
  
    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.pdfMail',
            with: $this->mailData
        );
    }
  
    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    public function attachments(): array
    {
        return [
            Attachment::fromData(fn () => $this->mailData['pdf']->output(), 'Report.pdf')
                ->withMime('application/pdf'),
        ];
    }
}

 

Step 5: Define Route

Now, we'll define routes into the web.php file

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PDFController;
  
Route::get('send-email-pdf', [PDFController::class, 'index']);

 

Step 6: Add Controller

Next, we'll create a controller and define the logic of create a pdf and sending mail.

app/Http/Controllers/PDFController.php

<?php
      
namespace App\Http\Controllers;
       
use Illuminate\Http\Request;
use App\Mail\PDFMailExample;
use PDF;
use Mail;
    
class PDFController extends Controller
{
       
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data["email"] = "test@gmail.com";
        $data["title"] = "From Techsolutionstuff";
        $data["body"] = "Send PDF Attachment";
    
        $pdf = PDF::loadView('emails.pdfMail', $data);
        $data["pdf"] = $pdf;
  
        Mail::to($data["email"])->send(new PDFMailExample($data));
    
        dd('Mail sent successfully');
    }
       
}

 

Step 7: Create View File

Then, we'll create a blade file for a PDF file.

resources/views/emails/pdfMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 11 Generate PDF and Send Email - Techsolutionstuff</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>{{ $body }}</p>
     
    <p>Thank you</p>
</body>
</html>

 

Step 8: Run the Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve

 


You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS