Laravel Send Email with Multiple Attachment Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi,

Now, let's see tutorial of laravel send email with multiple attachment. This post will give you simple example of laravel 8 send email with multiple attachment. I would like to share with you laravel send mail with multiple attachment. I explained simply step by step laravel mail multiple attachments.

Laravel provide mail class to send email. you can use several drivers for sending email in laravel. you can use smtp, Mailgun, Postmark, Amazon SES, and sendmail. you have to configure on env file what driver you want to use.

we can send email with multiple attachment in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. let's see bellow following steps.

Step 1: Install Laravel

I am going to explain step by step from scratch so, we need to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run 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 8 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}"

Step 3: Create Mail

In this step we will create mail class MyTestMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow command.

php artisan make:mail MyTestMail

app/Mail/MyTestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

class MyTestMail extends Mailable

{

use Queueable, SerializesModels;

public $details;

/**

* Create a new message instance.

*

* @return void

*/

public function __construct($details)

{

$this->details = $details;

}

/**

* Build the message.

*

* @return $this

*/

public function build()

{

$this->subject('Mail from ItSolutionStuff.com')

->view('emails.myTestMail');

foreach ($this->details['files'] as $file){

$this->attach($file);

}

return $this;

}

}

Step 4: Create Blade View

In this step, we will create blade view file and write email that we want to send. now we just write some dummy text. create bellow files on "emails" folder.

resources/views/emails/myTestMail.blade.php

<!DOCTYPE html>

<html>

<head>

<title>ItsolutionStuff.com</title>

</head>

<body>

<h1>{{ $details['title'] }}</h1>

<p>{{ $details['body'] }}</p>

<p>Thank you</p>

</body>

</html>

Step 5: 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\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-email', [EmailController::class, 'index']);

Step 6: Add Controller

Here,we require to create new controller EmailController that will manage index method of route.

make sure you have "files" folder in public with following files.

So let's put bellow code.

app/Http/Controllers/EmailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Mail;

use App\Mail\MyTestMail;

class EmailController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$files = [

public_path('files/160031367318.pdf'),

public_path('files/1599882252.png'),

];

$details = [

'title' => 'Mail from ItSolutionStuff.com 2',

'body' => 'This is for testing email using smtp',

'files' => $files

];

Mail::to('repaint@gmail.com')->send(new MyTestMail($details));

dd("Email is Sent.");

}

}

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/send-email

Output:

I hope it can help you...

Tags :
Shares