PDF Attachement With Laravel Mail.

Following this article, you will learn how to use Laravel mail compose with a pdf attachment. You may see the email sent with a pdf attachment from laravel. You will find this example of laravel mail attachment pdf helpful. I provided a straightforward explanation of Laravel's mail with a pdf attachment.


In applications built with Laravel 8, and Laravel 9, it is possible to send emails together with attachments.


For the sake of this demonstration, I shall only attach files while sending emails. To generate a basic example of delivering mail with files using the Laravel app, you must follow a few easy steps.


Let's look at the steps below:

Step 1: Install Laravel

I will explain everything in detail, starting from the very beginning. To begin, we need to create a new Laravel application by running the below commands. To do this, open your command prompt or terminal and type the following commands:


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

Step 2: Configure Email

The second step requires you to add the mail configuration in the .env file, including the email driver, host, mail port, username, and mail password. Laravel will utilize the specified configuration when sending emails. Consequently, all you need to do is add as follows:


MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myblog@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=myblog@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 3: Insert the Route

At this point, we must define routes. Open up your "routes/web.php" file and add the following route.


<?php

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', [PDFController::class, 'index']);

Step 4: Add Controller

In this step, first, we will create a new controller. Create a new PDFController With the following commands.


php artisan make:controller PDFController


Modify the PDFController file like the below code:


<?php
 
namespace App\Http\Controllers;
 
use PDF;
use Mail;
 
class PDFController extends Controller
{
    /**
    * Write code on Method
    *
    * @return response()
    */
    public function index()
    {
        $data["email"] = "test@gmail.com";
        $data["title"] = "From codesbright.com";
        $data["body"] = "This is Demo email";

        $files = [
            public_path(pdf/dummy_file1.pdf'),
            public_path('pdf/dummy_file2.png'),
        ];
 
        Mail::send('emails.test_email', $data, function($message)use($data, $files) {
            $message->to($data["email"], $data["email"])
                    ->subject($data["title"]);

            foreach ($files as $file){
                $message->attach($file);
            }
           
        });

        dd('Successfully send');
    }
}

Step 5: Add View File

In the last stage, let's construct test email.blade.php for the layout of the pdf file (resources/views/emails/test email.blade.php) and insert the following code:


<!DOCTYPE html>
<html>
<head>
    <title>codesbright.com</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <div>{{ $body }}</div>
   
    <p>Thank you</p>
</body>
</html>


Now run the application with the below commands:


php artisan serve


Call the route from your browser


localhost:8000/send-email


It will send an email with the PDF files.