Laravel Send Email with Multiple Attachment Example

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 and laravel 8 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

[email protected]

MAIL_PASSWORD=rrnnucvnqlbsl

MAIL_ENCRYPTION=tls

[email protected]

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 AppMail;

use IlluminateBusQueueable;

use IlluminateContractsQueueShouldQueue;

use IlluminateMailMailable;

use IlluminateQueueSerializesModels;

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 IlluminateSupportFacadesRoute;

use AppHttpControllersEmailController;

/*

|--------------------------------------------------------------------------

| 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 AppHttpControllers;

use IlluminateHttpRequest;

use Mail;

use AppMailMyTestMail;

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('[email protected]')->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:

Laravel Send Email with Multiple Attachment Example

I hope it can help you…

2 thoughts on “Laravel Send Email with Multiple Attachment Example”

Leave a Comment