Search code examples
phplaravelemailamazon-seslaravel-mail

laravel mail attach() method returns error about null argument



Hello, I'm making a mail service in laravel, that supports attachment upload, when the user send me the file, I store it in a Amazon S3 driver for further attachment. Here's my Mail class witch I send the mail object with the copies of email and attachments.

<?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');
    }
}

I've already used the dd(Storage::disk('s3Attachments')->url($attachment->path)) and confirmed that it is a string with the full path of the file like the documentation asks.

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:

Then When I run the code, it brings this error:

[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

I've tried using attach(), with just one argument:

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

But Same Error, I'm Using Laravel 5.5 with Amazon SES Driver, and already confirmed that I installed the following dependencies in my package.json:

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

I've searched the web and didn't find the solution, I need Help Please.

Regards.


Solution

  • I have just faced the exact same issue. The problem is that you override the $attachments property.

    Use another name for this variable and it will work!