Hi in this article i am going to show how to send multiple files attachment mail in Laravel. It’s simple example of Laravel send multiple attachment in mail. I explained simply step by step sending emails with multiple attachments.
so, let’s follow bellow steps:
Step 1 – Install Laravel Fresh Application
Use this command then download laravel project setup :
Step 2 - Set Mail Configuration You have to add your gmail smtp configuration, open your .env file and add your configration. .envcomposer create-project --prefer-dist laravel/laravel blog
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
<?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