Send attachment in mail in laravel 10

Send attachment in mail in laravel 10

| |
Image Carousel

Hello developers in this tutorial we will discuss about how to send attachment-pdf , image with email in laravel 

Table of Contents Contents-topics
1 Install PDF package
2 Setup PDF in laravel
3 Setup smtp driver in .env
4 Create pdf view blade
5 Setup EmailController
6 Setup routes.php
7 Sending email with attachment

1:Install PDF package

PDF means Portable Document Format that is used for exchanging documents  , to mail PDF attachments in Laravel is made possible by the laravel-pdf package , for installing package hit the command below on terminal.
code for pdf package : 

composer require niklasravnsborg/laravel-pdf

2:Setup PDF in laravel

For setting up PDF in laravel go to config/app.php register pdf provider and alias in app.php file 
Copy for creating artisan commandCopy

  'providers' => [
         /* Application Service Providers */
        'aliases' => Facade::defaultAliases()->merge([
         'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class

Now we have to publish the library 
code for publish 

php artisan vendor:publish

3:Setup smtp driver in .env

For sending email using SMTP driver we have to setup mail driver in .env file
smtp mail code:


Here  protected $signature = 'make:view {view}';  represents how to start this command and {view} means what should be name of our view file after that in handle() function consist of main logic behind the command to be executable.

4:Create pdf view blade

create a view file inside resources/views/sendEmail.blade.php and copy the code given below

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel 10 Email PDF Attachment Example</title>
    <body class="antialiased">
       <h2>A demo mail sent from</h2>
       <p>This is demo email send from DeveloperCodez </p>

 5:Setup EmailController

 Now for setting controller create a controller EmailCotroller

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class EmailController extends Controller
    public function index(Request $request){
            $data["email"] = "";
            $data["title"] = "DeveloperCodez";
            $data["body"] = "This is demo PDF email";
            $files = [
            Mail::send('mail.sendEmail', $data, function($message)use($data, $files) {
                foreach ($files as $file){
            echo "Mail successfully sent!";

6:Setup routes.php

Building new routes for sending email
code : 

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmailController;
| 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_pdf_mail', [EmailController::class, 'index']);

7:Sending email with attachment

php artisan serve


Tags: attach pdf with email in laravel 10 , send attachment with email in laravel 10 , laravel features , attach attachment with email in laravel 10 , send image with email in larvel 10,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More