Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.3-将多个文件附加到邮件_Laravel_Laravel 5_Laravel 5.3_Laravel Mail - Fatal编程技术网

Laravel 5.3-将多个文件附加到邮件

Laravel 5.3-将多个文件附加到邮件,laravel,laravel-5,laravel-5.3,laravel-mail,Laravel,Laravel 5,Laravel 5.3,Laravel Mail,如何将多个文件附加到laravel 5.3 mailable 在我的mailable构建方法中使用->attach($form->filePath)可以很容易地附加单个文件。但是,一旦将表单字段更改为数组,就会出现以下错误: basename() expects parameter 1 to be string, array given 我在stack上搜索了文档和各种搜索词,但都没有结果。任何帮助都将不胜感激 构建方法: public function build() { retur

如何将多个文件附加到laravel 5.3 mailable

在我的mailable构建方法中使用
->attach($form->filePath)
可以很容易地附加单个文件。但是,一旦将表单字段更改为数组,就会出现以下错误:

basename() expects parameter 1 to be string, array given
我在stack上搜索了文档和各种搜索词,但都没有结果。任何帮助都将不胜感激

构建方法:

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;
}
$attachments = [
    // first attachment
    'path/to/file1' => [
        'as' => 'file1.pdf',
        'mime' => 'application/pdf',
    ],

    // second attachment
    'path/to/file12' => [
        'as' => 'file2.pdf',
        'mime' => 'application/pdf',
    ],

    ...
];
在这种情况下,
$attachments
变量应该是一个带有文件路径的数组:

$attachments = [
    // first attachment
    '/path/to/file1',

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

此外,您不仅可以通过文件路径附加文件,还可以通过MIME类型和所需文件名附加文件,请参阅关于
附件
方法的第二种用法的文档:

例如,
$attachments
数组可以是这样的:

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 = [
    // first attachment
    'path/to/file1' => [
        'as' => 'file1.pdf',
        'mime' => 'application/pdf',
    ],

    // second attachment
    'path/to/file12' => [
        'as' => 'file2.pdf',
        'mime' => 'application/pdf',
    ],

    ...
];
可以从此阵列附加文件后:

// $attachments is an array with file paths of attachments
foreach($attachments as $filePath => $fileParameters){
    $email->attach($filePath, $fileParameters);
}

此解决方案在L5.8中非常有效,但请确保使用不同的名称,而不是
$attachments
,因为内部可邮件类已具有名为
$attachments
的属性。使用相同的名称将覆盖该属性并导致错误。@Sven,谢谢您的评论。但是,这个示例变量的名称是错误的。即使某些类有一个
$attachments
属性,我们仍然可以创建一个同名的变量
$attachments
,因为非静态属性可以通过伪变量
$this
,所以我们的变量不会干扰这个假设类的属性。任何类的静态属性都可以使用
Static
关键字访问,因此-我们的变量不会在那里造成任何问题。@AlexanderReznikov我不确定是什么问题,但可能有副作用:()如果您提到user@Matt E.试图创建自己的类属性
$attachments
,它会覆盖一个Laravel
$attachment
类属性。您可以毫无顾虑地使用“相同”名称创建变量,请参见一个示例:@AlexanderReznikov You's right。这只是
$this->attachments
的问题。谢谢你的耐心。