0

I want to send multiple images through email. I'm new to laravel and I do not know how add multiple attachment. But get below approach.

Send email controller:

foreach (json_decode($img_files) as $img_file) {
    $imgName = $img_file->name;
    $pathToFile[] = ['path' => public_path() . "\\uploads\\" . $imgName];
}

//send the email to the relevant customer email
Mail::to($customer_email)->send(new SendMail($customer_firstname, $pathToFile), function ($message) {
    // $message->attach($pathToFile);
    foreach ($pathToFile as $path) {
        $pathToFile = $path->path;

        $message->attach($pathToFile);
    }
});

Output of dd($pathToFile)

array:2 [
  0 => array:1 [
    "path" => "C:\xampp\htdocs\bit-project\public\uploads\photo_2019-11-08_23-11-50.jpg"
  ]
  1 => array:1 [
    "path" => "C:\xampp\htdocs\bit-project\public\uploads\photo_2019-11-08_23-11-55.jpg"
  ]
]

In the email template, this is how I add the image:

<img src="{{ $message->embed($pathToFile) }}" style="width:600px;height:335px">

I have an array with uploaded image paths as I mention above. How can I iterate over this array to pass it to SendMail. Appreciate your help!

(edit): This is the code for SendMail class

    public function __construct($customer_firstname, $pathToFile)
    {
        $this->customer_firstname = $customer_firstname;
        $this->pathToFile = $pathToFile;
        
    }

    public function build()
    {
       return $this->subject('Thank you! - Advance Car Diagnostics (Pvt.)Ltd')
                   ->view('admin.email_template')
                   ->with('data', $this->customer_firstname);
    }

I edited the build method as this:

    public function build()
    {

        $email = $this->subject('Thank you! - Advance Car Diagnostics (Pvt.)Ltd')
                     ->view('admin.email_template')
                     ->with('data', $this->customer_firstname);

        foreach($this->pathToFile as $filePath){
            $email->attach($filePath);
    
        }
        return $email;
    }

But it gives this error: basename () expects parameter 1 to be string array

Hashan
  • 184
  • 5
  • 22

0 Answers0