• Techblog369, India
  • June 8, 2023

How To Configure A Sendinblue SMTP Relay in Postfix?

Fix Email Issues in CyberPanel with SMTP Relay Setup Free Method. First Create a email using your Cyberpanel Under Emai Tab Create Email Second go to the SSL tab and click …

Create a simple password strength indicator with JavaScript

You’ve probably seen many examples of password strength indicators around the web. They let users know the password they’re using is weak and indicate how the strength changes when it’s modified. …

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 :

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


<?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

Author

nw.ippm@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *