How to Send Mail with PDF Attachment in Laravel

Hello artisan,

This amazing example tutorial will guide you laravel mail send with pdf attachment. We can see laravel email send with pdf attachment from this example. This example will teach you laravel mail attachment pdf. I will explain simple way of laravel send mail with pdf attachment.

You know that you can send emails with attachments in laravel 6, laravel 7, laravel 8 and laravel 9 applications with this source code. In this example, I will simply add files as attachments with sending emails in this example. If you want learn this then you just need to follow a few steps to create a simple example of sending mail with files in laravel app.

Let's see bellow steps:

laravel-send-email-with-attchment-example

 

 

Step 1: Install Laravel

I am going to explain step by step from scratch so, we need to get a fresh Laravel application using the bellow command, So open your terminal OR command prompt and run the bellow command:

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

 

Step 2: Make Configuration

In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, and mail password so laravel will use those sender details on email. So you can simply add like the following.

.env

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

 

Read also: Laravel 9  Activate Account after Email Verification Example

 

Step 3: Create Route

In this step, we need to create routes for an item listing. so open your "routes/web.php" file and add the following route.

routes/web.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-pdf', [PDFController::class, 'index']);

 

Step 4: Add Controller

Here, we require to create a new controller PDFController that will manage generate pdf method of the route. make sure you have a "files" folder in public with the following files. So let's put bellow code.

app/Http/Controllers/PDFController.php

namespace App\Http\Controllers;
  
use PDF;
use Mail;
  
class PDFController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $data["email"] = "mahedy150101@gmail.com";
        $data["title"] = "From codecheef.org";
        $data["body"] = "This is Demo";
 
        $files = [
            public_path('files/160031367318.pdf'),
            public_path('files/1599882252.png'),
        ];
  
        Mail::send('emails.myTestMail', $data, function($message)use($data, $files) {
            $message->to($data["email"], $data["email"])
                    ->subject($data["title"]);
 
            foreach ($files as $file){
                $message->attach($file);
            }
            
        });
 
        dd('Mail sent successfully');
    }
}

 

Step 5: Create View

In the Last step, let's create myTestMail.blade.php(resources/views/emails/myTestMail.blade.php) for the layout of a pdf file and put the following code:

resources/views/emails/myTestMail.blade.php

 

Read also: How to Add cc and bcc in Laravel Mail

 

Hope it can help you.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js