laravel 邮件 attach() 方法 returns 关于空参数的错误

laravel mail attach() method returns error about null argument


你好,我在laravel做一个邮件服务,支持附件上传,当用户发给我文件时,我将它存储在Amazon S3驱动程序中以供进一步附件。 这是我的邮件 class 我发送邮件对象以及电子邮件和附件的副本。

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Contracts\Queue\ShouldQueue;

   class Mail extends Mailable
   {
    use Queueable, SerializesModels;

    public $email;
    public $attachments;
    public $copies;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($email, $copies = [], $attachments = [])
    {
        $this->email = $email;
        $this->copies = $copies;
        $this->attachments = $attachments;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $m = $this->from($this->email->sender->email, $this->email->sender->name)
            ->replyTo($this->email->sender->email, $this->email->sender->name)
            ->subject($this->email->subject);

        foreach ($this->copies as $copy) {
            if ($copy->type == 'CC') {
                $m->cc($copy->destiny->email);
            } else {
                $m->bcc($copy->destiny->email);
            }
        }

        if (!empty($this->attachments)) {
            foreach ($this->attachments as $attachment) {
                $attachmentParameters = [
                    "as" => $attachment->name,
                    "mime" => $attachment->mime
                ];

                $m->attach(Storage::disk('s3Attachments')->url($attachment->path), $attachmentParameters);
            }
        }

        return $m->view('emails.text-plain');
    }
}

我已经使用了 dd(Storage::disk('s3Attachments')->url($attachment->path)) 并确认它是一个包含文件完整路径的字符串,如文档所要求的那样。

To add attachments to an email, use the attach method within the mailable class' build method. The attach method accepts the full path to the file as its first argument:

然后当我 运行 代码时,它带来了这个错误:

[2018-05-05 20:58:52] testing.ERROR: Type error: Argument 2 passed to Illuminate\Mail\Message::attach() must be of the type array, null given, called in /home/lefel/Sites/happymail/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php on line 311

我试过使用 attach(),只有一个参数:

$m->attach(Storage::disk('s3Attachments')->url($attachment->path));

但同样的错误,我正在使用 Laravel 5.5 和 Amazon SES 驱动程序,并且已经确认我在我的 package.json 中安装了以下依赖项:

composer require guzzlehttp/guzzle
"aws/aws-sdk-php": "~3.0"

我已经在网上搜索但没有找到解决方案,我需要帮助。

此致。

我刚刚遇到了完全相同的问题。 问题是您覆盖了 $attachments 属性.

为此变量使用另一个名称,它将起作用!