2014-09-18 47 views
1

如何發送laravel中的多個附件郵件?Laravel Mail ::發送多個附件

這是我laravel控制器:

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]')); 
} 

所有附件都在陣$path可用。

它顯示錯誤basename() expects parameter 1 to be string, array given

但是,當我使用$_POST['attach'] = $path[0];而不是$_POST['attach'] = $path;時,郵件只收到一個附件。

回答

11

就我所知,您可以對所有附件使用for循環。一些這樣的:

$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); 
+0

它可以正常使用您的建議...謝謝 – 2014-09-18 18:15:55

+1

$ message-> attach($ path [i]);應該是$ message-> attach($ path [$ i]); – 2016-04-27 12:53:54