DEV Community

Cover image for Laravel 12 Generate PDF and Send Email Tutorial
Msh Sayket
Msh Sayket

Posted on

Laravel 12 Generate PDF and Send Email Tutorial

In this tutorial, I will show you how to generate pdf file and send email attachments in laravel 12 application.

In this example, I will simply use dompdf to generate a PDF file and send an email with a PDF attachment. We will use Gmail SMTP for the mail driver. You just need to follow a few steps to create a simple example of sending an email with the created PDF file in a Laravel app.

Laravel 12 Generate PDF and Send Email Tutorial Example

Step 1: Install Laravel 12

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Install dompdf Package

First of all, we will install the barryvdh/laravel-dompdf composer package by following the composer command in your Laravel application.

composer require barryvdh/laravel-dompdf
Enter fullscreen mode Exit fullscreen mode

Read Also: Laravel 12 Custom User Login and Registration Tutorial

Step 3: Make Configuration

In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, and mail password so Laravel will use those sender details on the email. So you can simply add the following:
.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Mail Class
We are going from scratch, and in the first step, we will create an email for testing using the Laravel Mail facade. So let’s simply run the command below.

php artisan make:mail MailExample
Enter fullscreen mode Exit fullscreen mode

Now you will have a new folder “Mail” in the app directory with the MailExample.php file. So let’s simply copy the below code and paste it into that file. Read Full Tutorials

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)