Laravel 8 Send Email with PDF Attachment Tutorial

In this tutorial you will learn about the Laravel 8 Send Email with PDF Attachment Tutorial and its application with practical example.

In this Laravel 8 Send Email with PDF Attachment Tutorial I will show you how to send email with pdf attachment in laravel 8 application. In this tutorial you will learn to send email with pdf attachment in laravel. In this example we will be using niklasravnsborg/laravel-pdf package to generate pdf file to send as email attachment in laravel 8. We will also learn to generate pdf file using generate pdf library in laravel. In laravel mail class is used to send email. So, we would like to show you send an email in laravel 8. As well as we will also learn to send email in laravel 8 application using a mailable.

Laravel 8 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:

  • Step 1 – Install Laravel 8 App
  • Step 2 – Configuration SMTP in .env
  • Step 3 – Install PDF Library
  • Step 4 – Add Email Send Route
  • Step 5 – Create Directory And Blade View
  • Step 6 – Create Email Controller
  • Step 7 – Run Development Server

Step 1 – Install Laravel 8 App

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Step 2 – 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.

Step 3 – 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:

Step 4 – 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

Step 5 – 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:

Step 6 – 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 8 Send Email with PDF Attachment Tutorial and its application with practical example. I hope you will like this tutorial.