Laravel 5.3 - 将多个文件附加到 Mailables
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42848363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.3 - Attach Multiple Files To Mailables
提问by kash101
How does one go about attaching multiple files to laravel 5.3 mailable?
如何将多个文件附加到 Laravel 5.3 可邮寄?
I can attach a single file easily enough using ->attach($form->filePath)
on my mailable build method. However, soon as I change the form field to array I get the following error:
我可以使用->attach($form->filePath)
我的可邮寄构建方法轻松地附加单个文件。但是,一旦我将表单字段更改为数组,我就会收到以下错误:
basename() expects parameter 1 to be string, array given
I've searched the docs and also various search terms here on stack to no avail. Any help would be greatly appreciated.
我在堆栈上搜索了文档和各种搜索词,但无济于事。任何帮助将不胜感激。
Build Method:
构建方法:
public function build()
{
return $this->subject('Employment Application')
->attach($this->employment['portfolio_samples'])
->view('emails.employment_mailview');
}
Mail Call From Controller:
来自控制器的邮件呼叫:
Mail::to(config('mail.from.address'))->send(new Employment($employment));
回答by Alexander Reznikov
You should store your generated email as a variable, then you can just add multiple attachments like this:
您应该将生成的电子邮件存储为变量,然后您可以添加多个附件,如下所示:
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;
}
In this case your $attachments
variable should be an array with paths to files:
在这种情况下,您的$attachments
变量应该是一个包含文件路径的数组:
$attachments = [
// first attachment
'/path/to/file1',
// second attachment
'/path/to/file2',
...
];
此外,您不仅可以通过文件路径附加文件,还可以附加 MIME 类型和所需文件名,请参阅有关该
attachment
attachment
方法第二种使用情况的文档:https://laravel.com/docs/master/mail#attachmentshttps://laravel.com/docs/master/mail#attachmentsFor example, your $attachments
array can be something like this:
例如,您的$attachments
数组可以是这样的:
$attachments = [
// first attachment
'path/to/file1' => [
'as' => 'file1.pdf',
'mime' => 'application/pdf',
],
// second attachment
'path/to/file12' => [
'as' => 'file2.pdf',
'mime' => 'application/pdf',
],
...
];
After you can attach files from this array:
在您可以从这个数组附加文件之后:
// $attachments is an array with file paths of attachments
foreach($attachments as $filePath => $fileParameters){
$email->attach($filePath, $fileParameters);
}