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 Build Live Chat App In Laravel 9 And Vue JS
How To Build Live Chat App In...

In this article, we will see how to build a live chat app in laravel 9 and vue js. Here, we will learn how to creat...

Read More

Feb-02-2023

Target Class Does Not Exist In Laravel 8
Target Class Does Not Exist In...

In this article, we will see target class does not exist in laravel 8 and how to fix target class not found in...

Read More

Sep-23-2020

How To Generate RSS Feed In Laravel 9
How To Generate RSS Feed In La...

In this article, we will see how to generate an RSS feed in laravel 9. Here, we will generate an RSS feed in larave...

Read More

Dec-12-2022

Laravel 8 User Roles and Permissions Without Package
Laravel 8 User Roles and Permi...

In this tutorial we will see laravel 8 user roles and permissions without package.Roles and permissions are an impo...

Read More

Sep-13-2021