Share this:

Hello dev, Today we are going to learn Laravel Mail Send with PDF Attachment Example. This tutorial will cover on how to sending mail with pdf attachment in laravel application with example.

This extensive guide will teach you laravel mail send with pdf attachment. you can see laravel email send with pdf attachment. This example will help you laravel mail attachment pdf. I explained simply about laravel send mail with pdf attachment.

we can send emails with attachments in laravel 6, laravel 7, laravel 8 and laravel 9 application.

In this example, I will simply add files as attachments with sending emails. you just need to follow a few steps to create a simple example of sending mail with files in laravel app.

Let’s see below steps:

Also Read: How to set CC And BCC Email Address In Laravel Mail?

Steps on Laravel Mail Send with PDF Attachment

  • Step 1: Install Laravel
  • Step 2: Make Configuration
  • Step 3: Add Route
  • Step 4: Add Controller
  • Step 5: Create View File
  • Step 6: Testing

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 first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel will use those sender details on email. So you can simply add as like 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}"

Also Read: How to Check Running Laravel App Environment?

Step 3: Add Route

In this is step we need to create routes for items listing. so open your “routes/web.php” file and add following route.

routes/web.php

<?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 new controller PDFController that will manage generatePDF method of route.

make sure you have “files” folder in public with following files.

So let’s put bellow code.

app/Http/Controllers/PDFController.php

<?php
  
namespace App\Http\Controllers;
  
use PDF;
use Mail;
  
class PDFController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $data["email"] = "admin@laraveltuts.com";
        $data["title"] = "From LaravelTuts.com";
        $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');
    }
}

Also Read: How to Check If Request Has File in Laravel?

Step 5: Create View File

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

resources/views/emails/myTestMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Mail Send with PDF Attachment - LaravelTuts.com</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>{{ $body }}</p>
     
    <p>Thank you</p>
</body>
</html>

Now you can run and check example.

It will send you email, let’ see.

Step 6: Testing

Now everything is done! we can now test our the application. To test a application run the following code in terminal.

php artisan serve

Now, Open any web browser and open the following link.

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

Conclusion

Today, We had learn Laravel Mail Send with PDF Attachment Example. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: Laravel Blade Check if Array Key Exists Example

Share this:

Categorized in: