How to set dynamic SMTP details laravel

拈花ヽ惹草 提交于 2019-12-20 04:09:37

问题


I am working with a project where i need to update SMTP details on every admin login. I am storing the details in database, what is the best way to do that.


回答1:


My own approach: remove Illuminate\Mail\MailServiceProvider::class from config/app.php list of providers loaded at bootstrap, and create a new middleware to load it manually after the user has been identified.

<?php

namespace App\Http\Middleware;

use Illuminate\Contracts\Auth\Guard;  
use Illuminate\Mail\TransportManager;

use Closure;  
use Mail;  
use Config;  
use App;

class OverwriteMail  
{
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    public function handle($request, Closure $next)
    {
        /*
            $conf is an array containing the mail configuration,
            a described in config/mail.php. Something like:

            [
                'driver' => 'smtp',
                'host' => 'smtp.mydomain.com',
                'username' => foo',
                'password' => 'bar'
                ...
            ]
        */
        $conf = my_own_function();

        Config::set('mail', $conf);

        $app = App::getInstance();
        $app->register('Illuminate\Mail\MailServiceProvider');

        return $next($request);
    }
}

Source: http://blog.madbob.org/laravel-dynamic-mail-configuration/




回答2:


I think this should answer your question: https://laravel.io/index.php/forum/07-22-2014-swiftmailer-with-dynamic-mail-configuration

Just store it in a database table and use the Config facade to set the details on the fly.



来源:https://stackoverflow.com/questions/42626492/how-to-set-dynamic-smtp-details-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!