0

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

<?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))并确认它是一个带有文件完整路径的字符串,就像文档要求的那样。

要向电子邮件添加附件,请使用可邮寄类的构建方法中的 attach 方法。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"

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

问候。

4

1 回答 1

3

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

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

于 2019-05-21T11:32:09.573 回答