How To Generate PDF and Send Email In Laravel 8

    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
Read Also : How To Create Zip File In Laravel 7/8
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');
Read Also : How To Encrypt And Decrypt String In Laravel 8
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 :

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan