Send PDF as attachment in Email in Laravel 9
Digamber February 24, 2023
PDF offers the best way to exchange information between mails; If you are a laravel developer willing to know how to build a controller that helps you send PDF attachments with mail in Laravel, then this guide is for you.
PDF stands for Portable Document Format. It is a popular file format created by Adobe; the primary vision of the creator is to provide users with a facile yet reliable way to present and exchange documents.
We need a couple of things to send pdf attachments with email; you will need a laravel app, a mail trap for sending and testing emails with a pdf attachment, a controller, routes, and a view file.
Example: Laravel 9 Send PDF Attachment with Email
Build Laravel Project
A new laravel application can be created using Composer. If you have installed the Composer, then you can run the given command to build a new project.
> composer create-project laravel/laravel --prefer-dist laravel-blog
Install PDF Package
To mail PDF attachments in Laravel is made possible by the laravel-pdf package. This library does the core work for us; you have to install it to take advantage.
> composer require niklasravnsborg/laravel-pdf
Also see:
- Best ways to Set, Get and Delete Cookies using Laravel 9
- Projects In Laravel : Learn Laravel Building 10 Projects
- Learn the basics of Laravel 9
- Diploma in Laravel Application Development with Live Project App
- Laravel Application Development for Beginners
Set Up Laravel PDF
We have to move into the app.php
file that you can see in the config/
directory. You must register the service provider and facades in providers and aliases, respectively.
We are now going to run the command; our purpose is to publish the library config file to the config folder.
php artisan vendor:publish
Register SMTP in Env
In order to send a mail with pdf, you have to add the credentials inside the .env
config file. You can look for this file inside your laravel project.
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME= USER_NAME_GOES_HERE MAIL_PASSWORD= PASSWORD_GOES_HERE MAIL_ENCRYPTION=tls
You have to allow non-secure apps if you are willing to use Gmail, it will enable the less secure app; make sure to visit here
Set Up Send Email Controller
You have to run the command to generate a new controller. All the code with logic goes here into this file to build the desired feature.
php artisan make:controller PdfMailController
After invoking the above command, a new file is created; hence, you have to open the app/Http/Controllers/PdfMailController.php
file further and add the given code to the file.
to($data["email"]) ->subject($data["title"]); foreach ($files as $file){ $message->attach($file); } }); echo "Mail successfully sent!"; } } }
Create Send PDF Attachment View
Further, you have to set up the view file. You need to get into the resources/views
folder; here, you must make the home.blade.php
and add the given code to the file.
Laravel 9 Email PDF Attachment Example A mail sent from devstoc.com
Hey Dave, please find the attached PDF file…
Build New Routes
Go ahead and look for the resources/
folder; inside here, you will see web.php
file. Make sure to define the route that will help you shoot the mail with a pdf attachment.
Run Application Server
We have successfully created the functionality; now you need to run the server using given command. Make sure to test the feature using provided url.
php artisan serve
Open http://127.0.0.1:8000/send-pdf-via-mail
in your web browser.
Conclusion
Laravel PDF library is an mPDF wrapper especially built for Laravel 5 and higher. It quite easily generates PDF documents from HTML. In this extensive post, we taught you how to use the Laravel PDF library to send PDF attachments via email.