kas*_*101 13 laravel laravel-5 laravel-mail laravel-5.3
如何将多个文件附加到laravel 5.3 mailable?
我可以使用->attach($form->filePath)我的mailable构建方法轻松地附加单个文件.但是,当我将表单字段更改为数组时,我收到以下错误:
basename() expects parameter 1 to be string, array given
Run Code Online (Sandbox Code Playgroud)
我在堆栈上搜索文档和各种搜索术语无济于事.任何帮助将不胜感激.
构建方法:
public function build()
{
return $this->subject('Employment Application')
->attach($this->employment['portfolio_samples'])
->view('emails.employment_mailview');
}
Run Code Online (Sandbox Code Playgroud)
控制器的邮件呼叫:
Mail::to(config('mail.from.address'))->send(new Employment($employment));
Run Code Online (Sandbox Code Playgroud)
Ale*_*kov 22
您应该将生成的电子邮件存储在变量中,只需添加多个附件,如下所示:
public function build()
{
$email = $this->view('emails.employment_mailview')->subject('Employment Application');
// $attachments is an array with file paths of attachments
foreach($attachments as $filePath){
$email->attach($filePath);
}
return $email;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您的$attachments变量应该是一个包含文件路径的数组:
$attachments = [
// first attachment
'/path/to/file1',
// second attachment
'/path/to/file2',
...
];
Run Code Online (Sandbox Code Playgroud)
attachment方法的第二种使用情况的文档:https://laravel.com/docs/5.4/mail#attachments
例如,您的$attachments数组可以是这样的:
$attachments = [
// first attachment
'path/to/file1' => [
'as' => 'file1.pdf',
'mime' => 'application/pdf',
],
// second attachment
'path/to/file12' => [
'as' => 'file2.pdf',
'mime' => 'application/pdf',
],
...
];
Run Code Online (Sandbox Code Playgroud)
从这个数组中附加文件后:
// $attachments is an array with file paths of attachments
foreach($attachments as $filePath => $fileParameters){
$email->attach($filePath, $fileParameters);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9527 次 |
| 最近记录: |