Skip to content

All topics  /  Laravel

How To Send Multiple Files Attachment Mail In laravel?

Laravel ·

Teams applying “How To Send Multiple Files Attachment Mail In laravel?” in a production project can use the official Monitask overview to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hi Friends,

This article will give you example of how to send multiple files attachment mail in laravel. i explained simply about attach multiple files in laravel mailable. This tutorial will give you simple example of laravel mail markdown attechment mail.

In this post, i will show you laravel mail multiple attachments. we need to add so you laravel mail attach multiple files.i will so you laravel markdown mail component code and mail screenshot.

So let's see bellow solution:

Step 1 - Install Laravel Fresh Application


Use this command then download laravel project setup :

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

Step 2 - Set Mail Configuration

You have to add your gmail smtp configuration, open your .env file and add your configration.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls

Step 3 - Create Mailable Class with Markdown

php artisan make:mail SendEmail --markdown=emails.mail

app/Mail/SendEmail.php

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendEmail extends Mailable
{
    use Queueable, SerializesModels;
    public $maildata;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($maildata)
    {
        $this->maildata = $maildata;
    }
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $email = $this->markdown('emails.mail')->with('maildata', $this->maildata);
        $attachments = [
            // first attachment
            public_path('\images\img1.jpg'),
            // second attachment
            public_path('\images\img2.jpg'),
            // third attachment
            public_path('\images\img3.jpg'),
        ];
        // $attachments is an array with file paths of attachments
        foreach ($attachments as $filePath) {
            $email->attach($filePath);
        }
        return $email;
    }
}

Step 4 - Add Route

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MailController;
Route::get('send-mail', [MailController::class, 'sendMail']);

Step 5 - Create Controller

php artisan make:controller MailController

app/Http/Conrollers/MailController

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\SendEmail;
use Mail;
class MailController extends Controller
{	
	/**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
    */
    public function sendMail()
    {
        $email = '[email protected]';

   
        $maildata = [
            'title' => 'Laravel Mail Attach Multiple Files',
        ];
        Mail::to($email)->send(new SendEmail($maildata));

   
        dd("Mail has been sent successfully");
    }
}

Step 6 - Add View File

resources/views/emails/sendMail.blade.php

@component('mail::message')
# {{ $maildata['title'] }}
Hello Dev.
Thanks,
{{ config('app.name') }}
@endcomponent

You can run your project by using following command:

php artisan serve

Now open this url:

http://localhost:8000/send-mail

Output: