How does one go about attaching multiple files to laravel 5.3 mailable?

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:

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::to(config('mail.from.address'))->send(new Employment($employment));

推荐答案

您应该将生成的邮箱存储为变量,然后只需添加多个附件,如下所示:

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 = [
    // first attachment
    '/path/to/file1',

    // second attachment
    '/path/to/file2',
    ...
];

Also you can attach files not only by file paths, but with MIME type and desired filename, see documentation about second case of use for the `attachment` method: https://laravel.com/docs/master/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',
    ],
    
    ...
];

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

Laravel相关问答推荐

从8.0更新到10.0后,图像不再上传到存储

提交表格后如何在Livewire 3中调度模式窗口?

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

Laravel where 子句只返回一个数据库条目

Laravel | 使用select查询

如何防止 Laravel 路由被直接访问(即非 ajax 请求)

如何更新 Laravel 4 中现有的 Eloquent 关系?

在 laravel 4.2 中,用户 'root'@'localhost' 的 Laravel 访问被拒绝(使用密码:YES)

Laravel Blade - 产生内部部分

如何监控 Laravel 队列是否正在运行?

使用 Laravel 和 Passport 验证失败时响应状态码 401?

Laravel 5 - ErrorException 无法打开流:权限被拒绝

在 Laravel Blade 中转义 vue.js 数据绑定语法?

Laravel Artisan CLI 安全地停止守护进程队列工作者

Laravel 分页方法不适用于 map 集合?

为什么在 Laravel 的 DB::select 中使用 DB::raw?

laravel 队列 - 同步驱动程序如何工作?它是在单独的进程/线程还是主执行线程中执行?

如何获取 Laravel 块的返回值?

Eloquent的 attach/detach/sync 触发任何事件?

如何让 Laravel 将 View 的Content-Type标头返回为application/javascript?