简体   繁体   中英

Attach file to email in laravel 5

Im trying to attach a file to an email in laravel but i cant get it to work no matter what i do. It sends the email but with no attachment. doesn't give any errors neither.

public function sendCv($job)
{
    $cv = \App\File::where('id', Auth::user()->details->cv_id)->first();

    $cv_path = storage_path(). '/uploads/files/' . Auth::user()->id . "/" . $cv->identifier . "/" . $cv->name;

    $data = [];

    Mail::send('email.apply', $data, function ($message) use ($job, $cv_path) {
        $message->to('test@test.com')->from(Auth::user()->email)->subject('Application');
        $message->attach($cv_path);
    });

}

Its also setting the 'to' part of my email to 'Holidays in United Kingdom' in mac mail and i cant work out why?

在此处输入图片说明

Can anyone help and advise why this isn't working

EDIT:

Adding a second argument in the ->to method sorted out the holiday in the uk bit but with regards to troubleshooting i have already done.

I Dump and died the path to make sure it was correct and it is. I checked the file exists and it does. I made the path incorrect which then give me an error (which suggests its working with the correct path)

Set up a file downloading working with the same path and it worked fine

    $cv_path = storage_path(). '/uploads/files/' . Auth::user()->id . "/" . $cv->identifier . "/" . $cv->name;

    $headers = array(
        'Content-Type: application/pdf',
        'Content-Type: application/octet-stream',
    );

    return Response::download($cv_path, $cv->name, $headers);

this works fine and the file downloads.

if you want attach multi file :

 $path =[];
$path[] = '../path/your/number/one/file';
$path[] = '../path/your/number/two/file';

$data_mail = Mail::send($tmp, array('msg'=>$msg), function($message) use ($path) {
     $message->from('xxx@xxx.com', $_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]); // change i to $i

        }
  },true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM