How to Send PDF Attachment using Email in Laravel 9
This comprehensive tutorial will teach you how to send emails with PDF attachments in Laravel 9 using the laravel PDF package.
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.
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.
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.
Laravel 9 Send PDF Attachment with Email Example
- Step 1: Build Laravel Project
- Step 2: Install PDF Package
- Step 3: Set Up Laravel PDF
- Step 4: Register SMTP in Env
- Step 5: Set Up Send Email Controller
- Step 6: Create Send PDF Attachment View
- Step 7: Build New Routes
- Step 8: Run Application Server
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
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.
'providers' => [
/* Application Service Providers */
niklasravnsborg\LaravelPdf\PdfServiceProvider::class
],
'aliases' => Facade::defaultAliases()->merge([
'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class
])->toArray(),
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.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class PdfMailController extends Controller
{
public function index(Request $request){
{
$data["email"] = "demo@yahoo.com";
$data["title"] = "PositronX";
$data["body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$files = [
public_path('attachments/demo.jpeg'),
public_path('attachments/tariff_rates.pdf'),
];
Mail::send('mail.demo_mail', $data, function($message)use($data, $files) {
$message->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.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 9 Email PDF Attachment Example</title>
</head>
<body class="antialiased">
<h2>A demo mail sent from positronx.io</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam non ligula ligula. </p>
</body>
</html>
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.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfMailController;
/*
|--------------------------------------------------------------------------
| 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-pdf-via-mail', [PdfMailController::class, 'index']);
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
http://127.0.0.1:8000/send-pdf-via-mail
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.