【问题标题】:laravel mail attach() method returns error about null argumentlaravel mail attach() 方法返回关于空参数的错误
【发布时间】:2018-10-16 01:47:56
【问题描述】:

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

<?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')-&gt;url($attachment-&gt;path)),并确认它是一个包含文件完整路径的字符串,就像文档要求的那样。

要向电子邮件添加附件,请使用附件中的附加方法 可邮寄类的构建方法。 attach 方法接受完整路径 将文件作为其第一个参数:

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

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

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

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

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

问候。

【问题讨论】:

    标签: php laravel email amazon-ses laravel-mail


    【解决方案1】:

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

    为这个变量使用另一个名字,它会起作用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2014-06-19
      • 2016-07-31
      • 2021-09-17
      • 2016-08-31
      相关资源
      最近更新 更多