send file/s in email with laravel and Mail function

Tags: , ,



I´m traying to send file or files with function Mail in Laravel. And i´m a function to upload this file or files into a folder. My problem it´s i can´t to do that this file/s have send in one mail.

I have a <input type="file" multiple/> and with jquery i´m capturing his contain and it´s send to my controller where i´m doing a var_dump to show it. All contain it´s correct:

array(2) {
  [0]=>
  object(IlluminateHttpUploadedFile)#262 (7) {
    ["test":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    bool(false)
    ["originalName":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    string(11) "Captura.PNG"
    ["mimeType":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    string(9) "image/png"
    ["error":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    int(0)
    ["hashName":protected]=>
    NULL
    ["pathName":"SplFileInfo":private]=>
    string(24) "C:xampptmpphpA1D4.tmp"
    ["fileName":"SplFileInfo":private]=>
    string(11) "phpA1D4.tmp"
  }
  [1]=>
  object(IlluminateHttpUploadedFile)#266 (7) {
    ["test":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    bool(false)
    ["originalName":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    string(12) "Captura3.PNG"
    ["mimeType":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    string(9) "image/png"
    ["error":"SymfonyComponentHttpFoundationFileUploadedFile":private]=>
    int(0)
    ["hashName":protected]=>
    NULL
    ["pathName":"SplFileInfo":private]=>
    string(24) "C:xampptmpphpA1D5.tmp"
    ["fileName":"SplFileInfo":private]=>
    string(11) "phpA1D5.tmp"
  }
}
array(2) {
  [0]=>
  string(11) "Captura.PNG"
  [1]=>
  string(12) "Captura3.PNG"
}

But i can´t attach into email:

Mail::send('email', $datos, function($msj) use($subject, $emailUnico, $adjunto){

                    $msj->from("administrador@incidencias.integra.com","Incidencias");
                    $msj->subject($subject);
                    $msj->to($emailUnico);

                    for( $i=0; $i<count($adjunto); $i++ ){
                        $msj->attach($adjunto[$i], [
                            'as' => $adjunto[$i], // If you want you can chnage original name to custom name      
                            'mime' => 'application/pdf'
                        ]);
                    }

this is my function where i´m sending my email and attach mys file/s.

with this, i´m create a array with names and files from request:

for( $i=0; $i<count($request->file('adjunto')); $i++ ){
       $adjunto[] = $request->file('adjunto')[$i];
       $nombreAdjunto[] = $request->file('adjunto')[$i]->getClientOriginalName();
 }


 echo var_dump($adjunto);
 echo var_dump($nombreAdjunto);

 exit();

how i said before, all content it´s correct before i exposed my web browser console result.

i apreciatte all help with this problem.

Answer

I solve my problem. Only i would have to do this:

Mail::send('email', $datos, function($msj) use($subject, $emailUnico, $adjunto){

                    $msj->from("administrador@incidencias.integra.com","Incidencias");
                    $msj->subject($subject);
                    $msj->to($emailUnico);

                    for( $i=0; $i<count($adjunto); $i++ ){
                        $msj->attach(public_path()."/subidas/".$adjunto[$i]->getClientOriginalName(), [
                            'as' => $adjunto[$i]->getClientOriginalName(), // If you want you can chnage original name to custom name      
                            'mime' => 'application/pdf'
                        ]);
                    }
                

                });

add public_folder and in “as” add originalName



Source: stackoverflow