Skip to content

Mail::alwaysTo not working for synced mails #53614

@vatsake

Description

@vatsake

Laravel Version

11.24.1

PHP Version

8.3.6

Database Driver & Version

No response

Description

In my AppServiceProvider boot method I have

if (!App::isProduction()) {
    Mail::alwaysTo('John.Doe@mail.com');
}

I have a job that sends an email.

class SendEmail implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(public array|string $from, public string $subject = '', public string|array $to = [], public array|string $cc = [], public array|string $bcc = [], public string $content = '')
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        Mail::html($this->content, function (Message $message) {
            $message->to($this->to)
                ->bcc($this->bcc)
                ->cc($this->cc)
                ->subject($this->subject);
            if (is_array($this->from)) {
                $message->from(...$this->from);
            } else {
                $message->from($this->from);
            }
        });
    }
}

Steps To Reproduce

If sending email like this:
dispatch(new SendEmail(subject: 'test', to: 'Not.John.Doe@mail.com', content: 'test'));
The email will be sent to alwaysTo address.

If dispatching like this:
dispatch_sync(new SendEmail(subject: 'test', to: 'Not.John.Doe@mail.com', content: 'test'));
The email will NOT be sent to alwaysTo address...

BTW I'm using octane, so it might be 1 reason why it doesn't work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions