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
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
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}"
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
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
Top comments (0)