Home Laravel Laravel 9 Send Email with PDF Attachment

Laravel 9 Send Email with PDF Attachment

by Shahriar Sagor

Hello Dev , Today now in this post i will show you laravel 9 generate pdf and send email. let’s discuss about laravel 9 mail attachment pdf. Now I’m going to show you about the dompdf send email attachment laravel 9. I will use generate pdf and send mail in laravel 9.

Let’s see bellow steps:

Step 1: Install Laravel

At first of all we have to get the fresh Laravel version application by using the bellow command. So open your terminal OR command prompt and then run the bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Install dompdf Package

At first of all i will install barryvdh/laravel-dompdf composer package by using the following composer command in your laravel application.

composer require barryvdh/laravel-dompdf

After the successfully install package, need to open config/app.php file and then add service provider and alias.


'providers' => [
'aliases' => [
	'PDF' => Barryvdh\DomPDF\Facade::class,
Step 3: Make Configuration

Now in this step, i have to add send mail configuration with the mail driver, mail host, mail port, mail username, mail password . So laravel will be use those sender details on email. Now we can simply add as like as following.


Step 4: Add Route

Now in this is step i have to create routes for items listing. so need to open “routes/web.php” file and then add following route.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
| 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-email-pdf', [PDFController::class, 'index']);
Step 5: Add Controller

Here,i have to create a new controller PDFController that will manage index method of route. So let’s put the bellow code.


namespace App\Http\Controllers;
use PDF;
use Mail;
class PDFController extends Controller
     * Write code on Method
     * @return response()
    public function index()
        $data["email"] = "aatmaninfotech@gmail.com";
        $data["title"] = "From ItSolutionStuff.com";
        $data["body"] = "This is Demo";
        $pdf = PDF::loadView('emails.myTestMail', $data);
        Mail::send('emails.myTestMail', $data, function($message)use($data, $pdf) {
            $message->to($data["email"], $data["email"])
                    ->attachData($pdf->output(), "text.pdf");
        dd('Mail sent successfully');
Step 6: Create View File

Now in Last step, let’s create  i will create myTestMail.blade.php(resources/views/emails/myTestMail.blade.php) for the layout of pdf file and then put following code:


<!DOCTYPE html>
    <h1>{{ $title }}</h1>
    <p>{{ $body }}</p>
    <p>Thank you</p>

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook


You may also like