How to Send PDF Attachments using Email in Laravel 10

1. Create a PDF file:

First, you need to create a PDF file that you want to send as an attachment. You can use any PDF library or software to create the PDF file.

2. Store the PDF file:

Once you have created the PDF file, you need to store it in your Laravel application. You can store the PDF file in the public folder or in the storage folder.

3. Create a Mailable class:

Next, you need to create a Mailable class in your Laravel application. This class will be responsible for sending the email with the PDF attachment.

4. Add the PDF attachment:

In the Mailable class, you need to add the PDF attachment to the email. You can use the attach() method to add the PDF attachment to the email.

5. Send the email:

Finally, you need to send the email with the PDF attachment. You can use the Mail facade to send the email.

That’s it! You have successfully sent a PDF attachment using email in Laravel.

If you’re looking to send an email with a PDF attachment in a Laravel web application, this guide is just what you need. In this tutorial, you will learn how to send an attachment of a PDF file to an email using the Dompdf library in Laravel.

How to Send PDF Attachments using Email in Laravel 10

By using the following steps, you can send email attachments pdf in laravel 10 web apps with DOMPDF:

  • Step 1 – Create New Laravel 10 Project
  • Step 2 – Setup Database With Laravel App
  • Step 3 – Installing PDF Library
  • Step 4 – Add Email Send Route
  • Step 5 – Create a Directory And Blade View
  • Step 6 – Create Send Email Controller
  • Step 7 – Run Development Server

Step 1 – Create New Laravel 10 Project

Firstly, Open your terminal or command prompt.

And then execute the following command on terminal to install or download Laravel 10 application:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Setup Database With Laravel App

In this step, you need open .env file, which is located on laravel application root directory.

Then configure smtp details in .env file. like following:

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

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your Gmail for sending emails.

Step 3 – Installing PDF Library

In this step, you need to execute the following command on terminal or command prompt to install PDF package:

composer require niklasravnsborg/laravel-pdf

Here, we are using niklasravnsborg/laravel-pdf pckage to generate pdf.

Next add providers and aliases in app.php file, which is located inside config directory; as follows:

config/app.php

'providers' => [
	// ...
	niklasravnsborg\LaravelPdf\PdfServiceProvider::class
]
'aliases' => [
	// ...
	'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class
]

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

php artisan vendor:publish

Step 4 – Add Send Email Route

In this step, open /web.php, so navigate to the routes directory. And then add the following routes for sending emails with pdf attached:

use App\Http\Controllers\SendEmailController;

Route::get('send-email-pdf', [SendEmailController::class, 'index']);

Step 5 – Create a Directory And Blade View

In this step, create a test.blade.php blade view file inside resources/views/directory. And update the following code into it:

<!DOCTYPE html>
<html>
<head>
 <title>Laravel 10 Send Email Example</title>
</head>
<body>

 <h1>This is test mail from Tutsmake.com</h1>
 <p>Laravel 10 send email example</p>

</body>
</html> 

Then create a mail directory inside the resources\views directory. Then create test_mail.blade.php file inside mail directory and update the following code into it:

Hi, Tutsmake <br/>
This is Test Mail.<br />
Thank you...!!

Step 6 – Create Send Email Controller

In this step, execute the following command to create the controller name SendEmailController:

php artisan make:controller SendEmailController

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Mail;

use PDF;

class SendEmailController extends Controller
{
	
    public function index(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('mail.test_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

In this step, use this PHP artisan serve command to start your server locally:

php artisan serve

Then open browser and fire the following URL on it:

http://127.0.0.1:8000/send-email-pdf

Conclusion

Laravel 10 sends an email with a pdf example, you have learned how to send a pdf with an email attachment in Laravel 10.

Recommended Laravel Posts

If you have any questions or thoughts to share, use the comment form below to reach us.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?