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 Check Email Already Exist Or Not In Laravel
How To Check Email Already Exi...

In this article, we will show how to check whether the email already exists or not in laravel. Also, we will check...

Read More

Aug-07-2020

How To Validate Upload File Type Using Javascript
How To Validate Upload File Ty...

This article will show us how to validate upload file type using javascript. Using this post we can easily check the sel...

Read More

Aug-03-2020

How To Remove Spaces Using JQuery
How To Remove Spaces Using JQu...

In this article, we will explain to you how to remove extra space using jquery. many times we have requirements to...

Read More

Aug-10-2020

Laravel 9 QR Code Generator Example
Laravel 9 QR Code Generator Ex...

In this article, we will see laravel 9 QR code generator example. As per the current trend, many websites and appli...

Read More

Mar-25-2022