Laravel 9 Generate PDF And Send Email Example

Hello friends,

These tutorials show us how to generate laravel 9 pdf and after generating pdf send it in email. Let's discuss about laravel 9 mail generate pdf. I will use dompdf to generate pdf file and send mail with pdf attachment.

Nowadays it is important to create a pdf file, If you want to create a pdf file and send the file in an email, you are right place.

In this example, we will first generate a laravel 9 pdf file and then send that pdf file to email. To create a simple example of sending mail with a pdf file created in the laravel app you just need to follow a few steps one by one.

Step 1: Create L:aravel App

If you already have a newer app, you need to get the latest version of the application using the Belo command, run the following command

composer create-project laravel/laravel pdf-with-email

Step 2: Install dompdf Package

Here, we will install the barryvdh/laravel-dompdf composer package by following composer command in your tarminals.

composer require barryvdh/laravel-dompdf

After successfully installing the package, Goto the config/app.php file and add the service provider and alias.

config/app.php

'providers' => [
	..........
	..........

	Barryvdh\DomPDF\ServiceProvider::class,
],
  
'aliases' => [
	..........
	..........

	'PDF' => Barryvdh\DomPDF\Facade::class,
]

Step 3: Email Configuration For .env file

In this step, we will see about Mail Driver, Mail Host, Mail Port, Mail Username, Mail Password along with Send Mail Configuration will be required to send mail in laravel 9 so you can just add as below.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xxxxxx@gmail.com
MAIL_PASSWORD=xxxxxxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=xxxxxx@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 4: Create Route :

Here we create a way to get the nearest location from the latitude and long example

Goto routes/web.php file and edit bellow code

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\MailController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('send-email-with-pdf', [MailController::class, 'index']);

Step 5: Create Controller :

Now we need an area controller so run the following command to create a area controller

php artisan make:controller MailController

Goto app/Http/Controllers/MailController.php file and Generate a PDF and then send it to e-mail

app/Http/Controllers/MailController.php

<?php
  
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;
use Mail;
  
class MailController extends Controller
{
    /**
     * Generate a PDF and then send it to e-mail.
     *
     * @return \Illuminate\Http\Response
     */
    public function sendEmail(Request $request)
    {
        $data["email"] = "phpicoder@gmail.com";
        $data["title"] = "From Webthestuff.com";
        $data["body"] = "Hello. How to generate laravel 9 pdf and after generating pdf send it in email";
  
        $pdf = PDF::loadView('emails.demoMail', $data);
  
        Mail::send('emails.demoMail', $data, function($message)use($data, $pdf) {
            $message->to($data["email"], $data["email"])
                    ->subject($data["title"])
                    ->attachData($pdf->output(), "text.pdf");
        });
  
        dd('Mail sent successfully....');
    }
}

Step 6: Create View File

here, we need to create blade file for demoMail. so edit bellow code:

Goto resources/views/emails/demoMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Generate PDF With Send Email - Webthestuff.com</title>
</head>
<body>

    <h1>Hi...</h1>
    <h1>{{ $title }}</h1>
    <p>{{ $body }}</p>

    <p>Thank you.....</p>

</body>
</html>

Step 7: Run Laravel App :

This is the last step in which we run Laravel App. Now you have to type the following command and press enter to run the app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/pdf-with-email

Conclusion

Send an email with pdf attachment example, you learned how to create a pdf in laravel 9 and create a class that can be mailed and send a pdf with an email.

I hope this will help you to understand Generate PDF With Send Email in Laravel 9 app