In this tutorial you will learn about the Laravel 9 Send Email with PDF Attachment Tutorial and its application with practical example.
In this Laravel 9 Send Email with PDF Attachment Tutorial I will show you how to send email with pdf attachment in laravel 9 application. In this tutorial you will learn to send email with pdf attachment in laravel 9. In this example we will be using niklasravnsborg/laravel-pdf
package to generate pdf file to send as email attachment in laravel 9. We will also learn to generate pdf file using generate pdf library in laravel 9. In laravel mail class is used to send email. So, we would like to show you send an email in laravel 9. As well as we will also learn to send email in laravel 9 application using a mailable.
Laravel 9 Send Email with PDF Attachment Tutorial
In this step by step tutorial I will demonstrate you with example to send email with pdf attachment in laravel. Please follow the instruction given below:
- Install Laravel 9
- Configuration SMTP in .env
- Install PDF Library
- Add Email Send Route
- Create Directory And Blade View
- Create Email Controller
- Run Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Configuration SMTP in .env
In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .env file.
1 2 3 4 5 6 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=Add your user name here MAIL_PASSWORD=Add your password here MAIL_ENCRYPTION=tls |
Install PDF Library
In this step we will install a pdf library in laravel to send pdf file as email attahcment. we will install niklasravnsborg/laravel-pdf Package via the composer dependency manager. Use the following command to install niklasravnsborg/laravel-pdf Package.
1 |
composer require niklasravnsborg/laravel-pdf |
After Installing, we need to add service provider and alias in config/app.php file as following.
config/app.php
1 2 3 4 5 6 7 8 |
'providers' => [ // ... niklasravnsborg\LaravelPdf\PdfServiceProvider::class ] 'aliases' => [ // ... 'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class ] |
Now, execute the following command to publish package’s config file to your config directory by using following command:
1 |
php artisan vendor:publish |
Add Send Email Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 |
use App\Http\Controllers\SendEmailController; Route::get('send-email-pdf', [SendEmailController::class, 'index']); |
Create Directory And Blade View
In this step, we will create a directory name test inside resources/views directory. Then create an test.blade.php blade view file inside resources/views/ directory. And put the following code into it:
test.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <title>Laravel 9 Send Email Example</title> </head> <body> <h1>This is test mail</h1> <p>Laravel 9 send email example</p> </body> </html> |
Create Send Email Controller
Now, lets create a controller named SendEmailController using command given below –
1 |
php artisan make:controller SendEmailController |
Then go to app/Http/Controllers directory and open SendEmailController.php. Then put the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; use PDF; class SendEmailController extends Controller { public function sendmail(Request $request){ $data["email"]=$request->get("email"); $data["client_name"]=$request->get("client_name"); $data["subject"]=$request->get("subject"); $pdf = PDF::loadView('test', $data); try{ Mail::send('mails.mail', $data, function($message)use($data,$pdf) { $message->to($data["email"], $data["client_name"]) ->subject($data["subject"]) ->attachData($pdf->output(), "test.pdf"); }); }catch(JWTException $exception){ $this->serverstatuscode = "0"; $this->serverstatusdes = $exception->getMessage(); } if (Mail::failures()) { $this->statusdesc = "Error sending mail"; $this->statuscode = "0"; }else{ $this->statusdesc = "Message sent Succesfully"; $this->statuscode = "1"; } return response()->json(compact('this')); } } |
Step 7 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/send-email-pdf |