Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Mail::send with multiple attachment

Tags:

laravel

How can i send a mail with multiple attachments in laravel?

This is my laravel controller:

public function send_approve_mail($to, $subj, $tmp, $path) {

    $_POST['subj'] = $subj;
    $_POST['to'] = $to;

    foreach ($path as $key => $value) {
        $path[$key] = '../public/assets/fax/extra/' . $value;
    }

    $_POST['attach'] = $path;

    $msg = "test message here";
    $data_mail = Mail::send($tmp, array('msg' => $msg), function($message) {
                $message->from('[email protected]', $_POST['subj']);
                $message->to($_POST['to'])->subject($_POST['subj']);
                $message->attach($_POST['attach']);
            }, true);

    Help::send_mail($data_mail, array($_POST['to']), array('[email protected]'));
}

All attachments are available in array $path.

It's showing error basename() expects parameter 1 to be string, array given.

But when I use $_POST['attach'] = $path[0]; instead of $_POST['attach'] = $path;, mail is received with only one attachment.

like image 965
Yadhu Krishnan Avatar asked Jul 16 '26 07:07

Yadhu Krishnan


1 Answers

As far as my knowledge, you can just use a for loop for all the attachments. Some this like this:

$data_mail = Mail::send($tmp, array('msg'=>$msg), function($message) use ($path) {
    $message->from('[email protected]', $_POST['subj']);
    $message->to($_POST['to'])->subject($_POST['subj']);
    $size = sizeOf($path); //get the count of number of attachments 

    for ($i=0; $i < $size; $i++) {
        $message->attach($path[$i]);
    }
},true);
like image 57
Chintan Parekh Avatar answered Jul 17 '26 23:07

Chintan Parekh