2017-07-26 2 views
0

私のアプリケーションでは、各ユーザーは自分のSMTPサーバーを使用できます。したがって、設定を提供する必要があります。私はLaravel Notificationsを使って電子メールを送信しています。キューを使用していない場合(つまり同期を意味する)、問題はありません。キューに入れられた電子メールのLaravel 5.4でダイナミックSMTPデータを設定するにはどうすればよいですか?

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

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

問題ありません、今まで私は、元の設定を復元し、その後

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

私はCustomNotifiable形質を作りました。 しかし、それがキューに入れられている場合は、他のSMTP設定が提供されていても、キューワーカーを開始した後の最初の設定だけが以降のすべてのメールに適用されます。 config/mail.phpのデフォルト設定は無効になります。しかしこれは初めてのことです。

私はAppServiceProviderで作った::ブート方式(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(); 
     } 

    }); 

それは、スウィフトメーラーにキャッシュなどがあるようだ。新しいMailServiceProviderを登録しました。古いMailServiceProviderを置き換えるだけです。ですから、新しいSMTPデータでconfigを設定すると、登録された新しいプロバイダがそれらを受け取るべきです。設定を記録すると、正しいSMTPデータが設定され、メールを送信する直前にTransportManagerに表示されますが、メールは最初の設定で送信されました。

私はこのスレッドを見つけ、リンクされた解決策を試してみましたが、同じ結果と:How to set dynamic SMTP details laravel

だから私は、サービス/のServiceProvider/SMTP設定を無効にする方法が必要です。スーパーバイザがキューを再起動しても、異なるコンフィグレーションの複数の電子メールを同時に送信する機会があります。

答えて

0

多くのリサーチの後、私はさまざまなキューコマンドを見つけました。私は待ち行列を試しました:待ち行列の代わりに聞いた(これはLaravel 5.4の文書では説明されていません)。問題はなくなりました。

もちろん、これは記述された動作を実際に説明するものではありませんが、幸いにも問題はありません。私はこの解決策/回避策で暮らすことができます。

もう1つの異常な動作は、データベースがロックされているため、キューワーカーが例外をスローすることです。いいえ、これはいつ、なぜ起こったのでしょうか。

この投稿は、少し説明し、なぜ物事は起こることができます。一言で言えば、キュ​​ーにWhat is the difference between queue:work --daemon and queue:listen

を:だけでなく、私の問題は、別の非常に奇妙なDBロックの問題を解決聞いてください。

0

In Laravel 5。4 +、私はメーラークラスは、SMTPメールの設定を担当し、シングルトンでもあるMailTransportクラスを保持するシングルトンであることがわかります。

はまず、私はセットアップ形質が私はちょうどいくつかのメールのこの機能をオンにすることができます:私は、次のアプローチを使用して設定を無効にしなければならないのビルド()メソッドでは、

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']); 
     } 
    } 
} 

次に、あなたのあなたは上記の特性を利用して電話をすることができます:

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

ブーム、Laravelは残りをしましょう。

+0

この方法では、メールとパスワードのように、ポートとホストを設定できますか?もちろん男の – iglesiasedd

+1

は、クラスの実装を読んでください。 <.project> /vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php これらのメソッドは、$ mailTransport-> setHost( 'host')、$ mailTransport-> setPort( '1234')です。 –

関連する問題