Laravel邮件附件

时间:2018-10-25 11:49:24

标签: php laravel

我想在发送的邮件中附加多个文件。

邮件外观:

  public function build()
{
    return $this->from('noreply@webpts.co.za')
                ->to('johan.dutoit@nhls.ac.za')
                ->view('mails.demo')
                ->text('mails.demo_plain')
                ->with(
                  [
                        'testVarOne' => '1',
                        'testVarTwo' => '2',
                  ])
                  ->attach(public_path('/img').'/NHLS.jpg', [
                          'as' => 'NHLS.jpg',
                          'mime' => 'image/jpeg',
                        ]);
}

}

2 个答案:

答案 0 :(得分:0)

您可以将所有附件存储在数组中,然后循环遍历以附加所有附件

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

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

public function build()
{
    $email = $this->view('mails.demo')->subject('Test subject')
                ->from('noreply@webpts.co.za')
                ->to('johan.dutoit@nhls.ac.za')
                ->text('mails.demo_plain');

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

答案 1 :(得分:0)

要附加多个文件,只需将多个attach调用像这样->attach(...file1 params)->attach(...file2 params)依此类推