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

Tha*_*ars 3 swiftmailer laravel

在我的应用程序中,每个用户都可以使用自己的 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();
Run Code Online (Sandbox Code Playgroud)

之后,我恢复了原始配置:

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

        (new \Illuminate\Mail\MailServiceProvider(app()))->register();
Run Code Online (Sandbox Code Playgroud)

到现在都没问题。但是,如果它已排队,即使提供了任何其他 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();
            }
        }

    });
Run Code Online (Sandbox Code Playgroud)

当然,原始配置得到恢复:

    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();
        }

    });
Run Code Online (Sandbox Code Playgroud)

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

我找到了这个线程并尝试了链接的解决方案,但结果相同:How to set dynamic SMTP details laravel

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

小智 5

在 Laravel 5.4+ 中,我看到 Mailer 类是一个持有 MailTransport 类的单例,它负责 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']);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

    $this->changeMailSender([
        'email'=>$this->company->email,
        'password'=>$this->company->email_password,
    ]);
Run Code Online (Sandbox Code Playgroud)

Boom,剩下的就交给 Laravel 吧。