How To Generate PDF and Send Email In Laravel 8

Websolutionstuff | Dec-20-2021 | Categories : Laravel PHP

In this tutorial we will see how to generate pdf and send email in laravel 8. For generating PDF file we will use laravel-dompdf package, it is create pdf file and also provide to download file functionalities it is very easy to generate pdf in laravel 8 and for email sending we are use mailtrap.

Using this tutorial you can directly create invoice pdf and send it to email in laravel 8. laravel 8 send email with pdf attachment, laravel 8 send email with attachment, generate pdf and send mail in laravel 8.

Read More or Install Dompdf Package from Here : barryvdh / laravel-dompdf

Let's see how to generate pdf and send email in laravel 8.

Step 1 : Install Laravel

In this step we are install fresh laravel project using below command.

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

 

 

Step 2 : Install dom-pdf package and set mail configuration

Now, install barryvdh/laravel-dompdf package using below command.

composer require barryvdh/laravel-dompdf

Now open .env file and set email configuration.

MAIL_MAILER=smtp

MAIL_HOST=smtp.mailtrap.io

MAIL_PORT=2525

MAIL_USERNAME=your_username

MAIL_PASSWORD=your_password

MAIL_ENCRYPTION=TLS

MAIL_FROM_NAME="${APP_NAME}"

 

Step 2 : Add Route

Now, add route in your routes/web.php file.

Route::get('send/mail/pdf', [SendMailPDFController::class, 'sendMailWithPDF'])->name('send_mail_pdf');

 

 

Step 3 : Add Controller

Now create SendMailPDFController in your project and copy below code for attach file in mail.

<?php

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

class SendMailPDFController extends Controller
{
    public function sendMailWithPDF(Request $request)
    {
        $data["email"] = "[email protected]";
        $data["title"] = "How To Generate PDF And Send Email In Laravel 8 - Websolutionstuff";
        $data["body"] = "How To Generate PDF And Send Email In Laravel 8";

        $pdf = PDF::loadView('pdf_mail', $data);

        Mail::send('pdf_mail', $data, function ($message) use ($data, $pdf) {
            $message->to($data["email"], $data["email"])
                ->subject($data["title"])
                ->attachData($pdf->output(), "test.pdf");
        });

        echo "email send successfully !!";
    }
}

 

Step 4 : Create PDF view File

Now, in this step we will create the PDF view which will be attached to the email.

resources/views/pdf_mail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How To Generate PDF And Send Email In Laravel 8 - Websolutionstuff</title>
</head>
<body>    
    <h3> How To Generate PDF And Send Email In Laravel 8 </h3>

    <p> generate pdf and send mail in laravel 8 </p>

    <p> Thanks & Regards </p>
</body>
</html>

 


You might also like :

Recommended Post
Featured Post
Laravel 8 QR Code Generate Example
Laravel 8 QR Code Generate Exa...

In this post we will see Laravel 8 qr code generate example. we will generate QR Code using simple-qrcode package....

Read More

Jun-30-2021

How To Import CSV File In MySQL Using Node.js
How To Import CSV File In MySQ...

In this tutorial we will see how to import CSV file in MySQL using Node.js. Import and export CSV/EXCEL file in Nod...

Read More

Jul-30-2021

How To Generate Barcode Using Javascript
How To Generate Barcode Using...

In this article, we will see how to generate barcode using javascript. We will use a javascript plugin to generate...

Read More

Nov-01-2022

Mail: Laravel 11 Send Email using Queue
Mail: Laravel 11 Send Email us...

In this guide, we'll see how to send email using a queue in laravel 11. Here we'll see the concept of queue...

Read More

Apr-12-2024