title: How to set dynamic SMTP data in Laravel 5.4 for queued emails?
body: <p>In my application each user can use his own SMTP server. Therefor the config must be provided. I'm using Laravel Notifications to send the emails. If I'm using no queue (that means sync), there is no problem. </p>
  
  <p>I made a CustomNotifiable Trait:</p>
  
  <pre><code>        config([
              'mail.host' =&gt; $setting-&gt;smtp_host,
              'mail.port' =&gt; $setting-&gt;smtp_port,
              'mail.username' =&gt; $setting-&gt;smtp_username,
              'mail.password' =&gt; $setting-&gt;smtp_password,
              'mail.encryption' =&gt; $setting-&gt;smtp_encryption,
              'mail.from.address' =&gt; $setting-&gt;smtp_from_address,
              'mail.from.name' =&gt; $setting-&gt;smtp_from_name,
          ]);
  
          (new \Illuminate\Mail\MailServiceProvider(app()))-&gt;register();
  </code></pre>
  
  <p>After that, I restore the original config:</p>
  
  <pre><code>        config([
              'mail' =&gt; $originalMailConfig
          ]);
  
          (new \Illuminate\Mail\MailServiceProvider(app()))-&gt;register();
  </code></pre>
  
  <p>No problem until now.
  But if it's queued, just the first config after starting the queue worker will be taken for all further emails, even if any other SMTP config is provided. The default config from config/mail.php will be overridden. But this only works the first time.</p>
  
  <p>I've made in the AppServiceProvider::boot method (the SMTP config is stored at the notification):</p>
  
  <pre><code>    Queue::before(function (JobProcessing $event) {
  
          // Handle queued notifications before they get executed
          if (isset($event-&gt;job-&gt;payload()['data']['command']))
          {
              $payload = $event-&gt;job-&gt;payload();
              $command = unserialize($payload['data']['command']);
  
              // setting dynamic SMTP data if required
              if (isset($command-&gt;notification-&gt;setting))
              {
                  config([
                      'mail.host' =&gt; $command-&gt;notification-&gt;setting-&gt;smtp_host,
                      'mail.port' =&gt; $command-&gt;notification-&gt;setting-&gt;smtp_port,
                      'mail.username' =&gt; $command-&gt;notification-&gt;setting-&gt;smtp_username,
                      'mail.password' =&gt; $command-&gt;notification-&gt;setting-&gt;smtp_password,
                      'mail.encryption' =&gt; $command-&gt;notification-&gt;setting-&gt;smtp_encryption,
                      'mail.from.address' =&gt; $command-&gt;notification-&gt;setting-&gt;smtp_from_address,
                      'mail.from.name' =&gt; $command-&gt;notification-&gt;setting-&gt;smtp_from_name,
                  ]);
  
                  (new \Illuminate\Mail\MailServiceProvider(app()))-&gt;register();
              }
          }
  
      });
  </code></pre>
  
  <p>Of course, the original config get restored:</p>
  
  <pre><code>    Queue::after(function (JobProcessed $event) use ($originalMailConfig) {
  
          $payload = $event-&gt;job-&gt;payload();
          $command = unserialize($payload['data']['command']);
  
          // restore global mail settings
          if (isset($command-&gt;notification-&gt;setting))
          {
              config([
                  'mail' =&gt; $originalMailConfig
              ]);
  
              (new \Illuminate\Mail\MailServiceProvider(app()))-&gt;register();
          }
  
      });
  </code></pre>
  
  <p>It seems, as the Swift Mailer has a cache or something like that. I registered a new MailServiceProvider, which should simply replace the old one. So if I set the config with the new SMTP data, the new registered provider should take them. Logging the config shows even in the TransportManager, that the correct SMTP data were set, right before sending the mail, but the mail was sent with the first set config. </p>
  
  <p>I found this thread and tried the linked solution, but with the same result: <a href="https://stackoverflow.com/questions/42626492/how-to-set-dynamic-smtp-details-laravel">How to set dynamic SMTP details laravel</a></p>
  
  <p>So I need a way to override the Services / ServiceProvider / SMTP config. Even if the Supervisor restarts the queue, there is a chance that multiple emails with different configs should be send at the same time.</p>