set mail driver dynamically from database for different email in notification



PHP Snippet 1:

config([
    'mail.driver' => 'smtp',
    'mail.host' => 'smtp.mailgun.org',
    'mail.port' => 587,
    'mail.encryption' => 'tls',
    'mail.username' => 'if-any',
    'mail.password' => 'if-any',
]);

PHP Snippet 2:

    $swiftMailer = app('mailer')->getSwiftMailer();

    $transport = $swiftMailer->getTransport()
        ->setUsername($username)
        ->setPassword($password)
        ->setHost($host)
        ->setPort($port)
        ->setEncryption($encryption)
        ->setTimeout(30);

    $mailer = app(\Illuminate\Mail\Mailer::class);
    $mailer->setSwiftMailer(new \Swift_Mailer($transport));
    $mailer->alwaysReplyTo($reply_to);
    $mailer->to($user->email)
        ->send(new AdminPasswordReset($data));