laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.62k stars 11.03k forks source link

Mail::alwaysTo not working for synced mails #53614

Open vatsake opened 23 hours ago

vatsake commented 23 hours ago

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.

github-actions[bot] commented 22 hours ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!