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
How To Send Email With Attachment In Laravel 9
How To Send Email With Attachm...

In this article, we will see how to send email with attachments in laravel 9. As we all know mail functionalities are co...

Read More

Mar-16-2022

How to Install Select2 in Laravel 10 Example
How to Install Select2 in Lara...

Hey there, Laravel enthusiasts! If you're looking to add a touch of elegance and user-friendliness to your web...

Read More

Feb-14-2024

Next Previous Link Button Pagination in Laravel
Next Previous Link Button Pagi...

Today we will learn next previous link button pagination in laravel, Using paginate method you can easily create paginat...

Read More

Jun-14-2021

How To Image Upload Using Ajax In Laravel 9
How To Image Upload Using Ajax...

In this article, we will see how to image upload using ajax in laravel 9. Here, we will learn about image upload in...

Read More

Feb-07-2023