How to send multiple attachment to mail in Laravel?

· · 1497 views

I am able to send email. However, I don't know how to send multiple attachments(images/pdf/docs). Any help would be appreciated. I am using laravel 7.

0
2 Answers

The Illuminate\Mail\Mailable::attach() method returns $this, you just have to chain it:

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('emails.orders.shipped')
                ->attach('/path/to/file', [
                    'as' => 'name.pdf',
                    'mime' => 'application/pdf',
                ])
		->attach('/path/to/another/file')
		->attach('/path/to/a/third/file');
}
0

The following code is works perfect for me, and I can send multiple attachment with the email. So here is the code:

Here is my SendMail mailable class:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendMail extends Mailable
{
    use Queueable, SerializesModels;
    public $body;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($request)
    {
              $this->body = $request->body;
        	$this->attachment = $request->file;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {

	$mail = $this->subject('Mail from Real Programmer')->view('admin.emails.sendmail');
	
	if ($this->attachment) {
        foreach($this->attachment as $file)
            $mail->attach($file->getRealPath(), [
                'as' => $file->getClientOriginalName(), 
                'mime' => $file->getMimeType()
            ]);
        }
    }
}

Here is the controller where I received the post request. This post request has multiple files uploads, And I am sending the whole request to my sendmail.php

public function mailsend(Request $request)
{
	$people = User::get();
    
	foreach($people as $p){
		Mail::to($p->email)
     		->send(new SendMail($request));
		}
	}
		
     return redirect()->back();
}

Hope it helped to you.

0

Please login or create new account to participate in this conversation.