How To Generate PDF and Send Email In Laravel 8

In this article, we will see how to generate a pdf and send an email in laravel 8. For generating PDF files we will use the "laravel-dompdf" package also we will use "mailtrap" for email sending. dompdf creates a pdf file and also provides download file functionalities. it is very easy to generate pdf in laravel 8. Using this tutorial you can directly create an invoice pdf and send it to email in laravel 8.

So, let's see generate a pdf file and send mail in laravel 8.

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

Step 1: Install Laravel 8

In this step, we will install the laravel application using the below command.

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

 

 

Step 2: Install the dom-pdf package

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

composer require barryvdh/laravel-dompdf

 

Step 3: Set mail configuration

Now, open the .env file and set the email configuration. Create a mailtrap account for sending mail.

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 4: Add Route

In this step, we will add routes in your routes/web.php file.

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

 

 

Step 5: Add Controller

Now, we will create SendMailPDFController and add the below code for sending mail with attachments.

<?php

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

class SendMailPDFController extends Controller
{
    public function sendMailWithPDF(Request $request)
    {
        $data["email"] = "test@gmail.com";
        $data["title"] = "Laravel 8 send email with attachment - Techsolutionstuff";
        $data["body"] = "Laravel 8 send email with attachment";

        $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 6: Create PDF View File

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>Laravel 8 Send Email With Attachment - Techsolutionstuff</title>
</head>
<body>    
    <h3> Laravel 8 Send Email With Attachment </h3>

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

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

 


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