Laravel 5.3-将多个文件附加到可邮件 [英] Laravel 5.3 - Attach Multiple Files To Mailables

查看:118
本文介绍了Laravel 5.3-将多个文件附加到可邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个文件附加到laravel 5.3可邮寄?

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

我可以在可邮寄的构建方法上使用->attach($form->filePath)轻松地附加单个文件.但是,将表单字段更改为数组后,会出现以下错误:

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.

构建方法:

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

推荐答案

您应该将生成的电子邮件存储为变量,然后可以像这样添加多个附件:

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

在这种情况下,您的$attachments变量应该是一个包含文件路径的数组:

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',
    ...
];


另外,您不仅可以按文件路径附加文件,还可以附加MIME类型和所需文件名,请参见有关attachment方法的第二种使用情况的文档:


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数组可以是这样的:

For example, your $attachments array can be something like this:

$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 5.3-将多个文件附加到可邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆