如何在 Laravel 5.4 中为排队的电子邮件设置动态 SMTP 数据?

How to set dynamic SMTP data in Laravel 5.4 for queued emails?

在我的应用程序中,每个用户都可以使用自己的 SMTP 服务器。因此必须提供配置。我正在使用 Laravel 通知来发送电子邮件。如果我不使用队列(即同步),则没有问题。

我做了一个 CustomNotifiable Trait:

        config([
            'mail.host' => $setting->smtp_host,
            'mail.port' => $setting->smtp_port,
            'mail.username' => $setting->smtp_username,
            'mail.password' => $setting->smtp_password,
            'mail.encryption' => $setting->smtp_encryption,
            'mail.from.address' => $setting->smtp_from_address,
            'mail.from.name' => $setting->smtp_from_name,
        ]);

        (new \Illuminate\Mail\MailServiceProvider(app()))->register();

之后我恢复原来的配置:

        config([
            'mail' => $originalMailConfig
        ]);

        (new \Illuminate\Mail\MailServiceProvider(app()))->register();

到目前为止没问题。 但是,如果它已排队,即使提供了任何其他 SMTP 配置,也只会为所有进一步的电子邮件采用启动队列工作程序后的第一个配置。来自 config/mail.php 的默认配置将被覆盖。但这只适用于第一次。

我在AppServiceProvider::boot方法中做了(SMTP配置存储在通知中):

    Queue::before(function (JobProcessing $event) {

        // Handle queued notifications before they get executed
        if (isset($event->job->payload()['data']['command']))
        {
            $payload = $event->job->payload();
            $command = unserialize($payload['data']['command']);

            // setting dynamic SMTP data if required
            if (isset($command->notification->setting))
            {
                config([
                    'mail.host' => $command->notification->setting->smtp_host,
                    'mail.port' => $command->notification->setting->smtp_port,
                    'mail.username' => $command->notification->setting->smtp_username,
                    'mail.password' => $command->notification->setting->smtp_password,
                    'mail.encryption' => $command->notification->setting->smtp_encryption,
                    'mail.from.address' => $command->notification->setting->smtp_from_address,
                    'mail.from.name' => $command->notification->setting->smtp_from_name,
                ]);

                (new \Illuminate\Mail\MailServiceProvider(app()))->register();
            }
        }

    });

当然,原来的配置得到恢复:

    Queue::after(function (JobProcessed $event) use ($originalMailConfig) {

        $payload = $event->job->payload();
        $command = unserialize($payload['data']['command']);

        // restore global mail settings
        if (isset($command->notification->setting))
        {
            config([
                'mail' => $originalMailConfig
            ]);

            (new \Illuminate\Mail\MailServiceProvider(app()))->register();
        }

    });

看来,Swift 邮件程序有一个缓存或类似的东西。我注册了一个新的 MailServiceProvider,它应该只是替换旧的。因此,如果我使用新的 SMTP 数据设置配置,新注册的提供商应该接受它们。记录配置甚至在 TransportManager 中显示,在发送邮件之前设置了正确的 SMTP 数据,但是邮件是使用第一个设置的配置发送的。

我找到了这个帖子并尝试了链接的解决方案,但结果相同:

所以我需要一种方法来覆盖 Services / ServiceProvider / SMTP 配置。即使 Supervisor 重新启动队列,也有可能同时发送多封不同配置的邮件。

经过大量研究,我偶然发现了不同的队列命令。我尝试了 queue:listen(Laravel 5.4 文档中没有描述)而不是 queue:work,问题就消失了。

当然,这并不能真正解释所描述的行为,但幸运的是这并不重要,因为我可以忍受这个solution/workaround。

另一个奇怪的行为是,队列工作者不时抛出异常,因为数据库被锁定。不知道,何时或为何发生这种情况。

这个post解释了一点,为什么事情会发生:What is the difference between queue:work --daemon and queue:listen

简而言之,queue:listen 解决了我的问题以及另一个非常奇怪的数据库锁定问题。

在Laravel 5.4+中,正如我所见,Mailer Class 是一个包含 MailTransport Class 的单例,它负责 SMTP 邮件的配置并且是一个单例,也;我只需要使用以下方法覆盖配置:

首先,我设置了一个特征,这样我就可以在一些邮件上打开这个功能:

trait MailSenderChangeable
{
    /**
     * @param array $settings
     */
    public function changeMailSender($settings)
    {
        $mailTransport = app()->make('mailer')->getSwiftMailer()->getTransport();
        if ($mailTransport instanceof \Swift_SmtpTransport) {
            /** @var \Swift_SmtpTransport $mailTransport */
            $mailTransport->setUsername($settings['email']);
            $mailTransport->setPassword($settings['password']);
        }
    }
}

然后,在您的邮件 class 的 build() 方法中,您可以利用上述特征并调用:

    $this->changeMailSender([
        'email'=>$this->company->email,
        'password'=>$this->company->email_password,
    ]);

繁荣,让 Laravel 完成剩下的工作。