Laravel 9 Send Email with PDF Attachment Tutorial

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

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.

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.

After Installing, we need to add service provider and alias in config/app.php file as following.

config/app.php

Now, execute the following command to publish package’s config file to your config directory by using following command:

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

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

Create Send Email Controller

Now, lets create a controller named SendEmailController using command given below –

Then go to app/Http/Controllers directory and open SendEmailController.php. Then put the following code into it:

Step 7 – Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the Laravel 9 Send Email with PDF Attachment Tutorial and its application with practical example. I hope you will like this tutorial.