skip to Main Content

I have the following mailable:

<?php

namespace AppMail;

use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

class UserLoginDetailsMail extends Mailable
{
    use Queueable, SerializesModels;

    protected String $username;
    protected String $email;
    protected String $password;

    /**
     * Create a new message instance.
     *
     * @param String $username
     * @param String $email
     * @param String $password
     */
    public function __construct(String $username, String $email, String $password)
    {
        $this->username = $username;
        $this->email = $email;
        $this->password = $password;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
            ->from('[email protected]', env('TENANT_NAME'))
            ->replyTo(env('TENANT_EMAIL'))
            ->to($this->email)
            ->subject('Test')
            ->view('api.emails.user-login-details')
            ->with([
                'username' => $this->username,
                'password' => $this->password,
            ]);
    }
}

Being fired like so:

Mail::queue(new UserLoginDetailsMail('test', "[email protected]", "test"));

However my queued job is experiencing the following error:

ErrorException: Trying to get property 'email' of non-object in /vendor/laravel/framework/src/Illuminate/Mail/Mailable.php:612

/**
     * Set the recipients of the message.
     *
     * All recipients are stored internally as [['name' => ?, 'address' => ?]]
     *
     * @param  object|array|string  $address
     * @param  string|null  $name
     * @param  string  $property
     * @return $this
     */
    protected function setAddress($address, $name = null, $property = 'to')
    {
        foreach ($this->addressesToArray($address, $name) as $recipient) {
            $recipient = $this->normalizeRecipient($recipient);

            $this->{$property}[] = [
                'name' => $recipient->name ?? null,
                'address' => $recipient->email,
            ];
        }

        return $this;
    }

This queued mail is being fired within another queued job, not sure if this would have any impact.

Thanks

2

Answers


  1. The code looks fine. You probably made changes in your code though, without restarting the queue. As per the Laravel documentation:

    Since queue workers are long-lived processes, they will not notice changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart command:

    php artisan queue:restart
    
    Login or Signup to reply.
  2. Did you cache your config? If so clean it with php artisan config:clear and try to send mail again.

    it’s will work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search