Send Multiple Files Attachment Mail using Laravel

Jan 07, 2022 . Admin



Hello Dev,

This tutorial will give you example of send multiple files attachment mail laravel. i would like to share with you attach multiple files to mailables. it's simple example of laravel send multiple attachment in mail. i explained simply step by step sending emails with multiple attachments.

i am going to step by step from how to create new laravel send multiple files attachment mail.

so, let's follow bellow steps:

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
<?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 = 'xyz@gmail.com';
   
        $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:



It will help you...
#Laravel 8 #Laravel