【问题标题】:How to change mail configuration before sending a mail in the controller using Laravel?如何在使用 Laravel 在控制器中发送邮件之前更改邮件配置?
【发布时间】:2014-06-19 17:45:52
【问题描述】:

我正在使用 Laravel 4,我想更改控制器中的邮件配置(如驱动程序/主机/端口/...),因为我想将配置文件保存在具有不同邮件配置的数据库中。这是使用 config/mail.php 配置的基本发送邮件

Mail::send(
    'emails.responsable.password_lost',
    array(),
    function($message) use ($responsable){
        $message->to($responsable->email, $responsable->getName());
        $message->subject(Lang::get('email.password_lost'));
    });

我试过放类似的东西,但没有用

 $message->port('587');

感谢您的支持!

【问题讨论】:

    标签: php email laravel


    【解决方案1】:

    如果您想创建一个Laravel 7 应用程序,允许用户在您的应用程序上注册和登录,并且您打算使每个用户能够通过您的平台发送电子邮件,使用他们自己的唯一电子邮件地址和密码.

    解决方案:

    1. Laravel Model:首先您需要创建一个数据库表来存储用户的电子邮件配置数据。接下来,您需要一个 Eloquent 模型 来检索经过身份验证的用户的 id 以动态获取他们的电子邮件配置数据。
    2. Laravel ServiceProvider:接下来,创建一个服务提供者,它将使用 Model 类中的范围方法查询数据库以获取用户的电子邮件配置,并将其设置为他们的默认邮件配置。不要在您的config/app.php 中注册此服务提供商
    3. Laravel MiddleWare:还创建一个中间件,在用户通过身份验证并注册 ServiceProvider 时运行。

    实现模型

    进行迁移。从命令行php artisan make:migration create_user_email_configurations_table 运行这些。那么:

    Schema::create('user_email_configurations', function (Blueprint $table) {
      $table->id();
      $table->string('user_id');
      $table->string('name');
      $table->string('address');
      $table->string('driver');
      $table->string('host');
      $table->string('port');
      $table->string('encryption');
      $table->string('username');
      $table->string('password');
      $table->timestamps();
    });
    

    完成并创建您的模型。运行php artisan migratephp artisan make:model userEmailConfiguration。现在在你的模型中添加一个作用域方法。

    <?php
      namespace App;
      use Illuminate\Support\Facades\Auth;
      use Illuminate\Database\Eloquent\Model;
    
    class userEmailConfiguration extends Model
    {
      protected $hidden = [
        'driver',
        'host',
        'port',
        'encryption',
        'username',
        'password'
      ];
      public function scopeConfiguredEmail($query) {
        $user = Auth::user();
        return $query->where('user_id', $user->id);
      }
    }
    

    实施服务提供者

    从命令行运行它 - php artisan make:provider MailServiceProvider

    <?php
      namespace App\Providers;
      use Illuminate\Support\ServiceProvider;
      use App\userEmailConfiguration;
      use Config;
    class MailServiceProvider extends ServiceProvider
    {
      public function register()
      {
        $mail = userEmailConfiguration::configuredEmail()->first();
        if (isset($mail->id))
        {
          $config = array(
            'driver'     => $mail->driver,
            'host'       => $mail->host,
            'port'       => $mail->port,
            'from'       => array('address' => $mail->address, 'name' => $mail->name),
            'encryption' => $mail->encryption,
            'username'   => $mail->username,
            'password'   => $mail->password
          );
          Config::set('mail', $config);
        }
      }
      public function boot()
      {
      }
    }
    

    实现中间件

    运行以下命令 - php artisan make:middleware MailService

    <?php
      namespace App\Http\Middleware;
      use Closure;
      use App;
    class MailService
    {
      public function handle($request, Closure $next)
      {
        $app = App::getInstance();
        $app->register('App\Providers\MailServiceProvider');
        return $next($request);
      }
    }
    

    现在我们已经实现了所有这些,在 $routedMiddleware 数组中的 kennel.php 中将中间件注册为 mail。然后在经过身份验证的路由中间件中调用它:

    示例:

    Route::group(['middleware' => [ 'auth:api' ]], function () {
      Route::post('send/email', 'Controller@test_mail')->middleware('mail');
    });
    

    这是我在媒体上发布的原始帖子 - Enable Unique And Dynamic SMTP Mail Settings For Each User — Laravel 7

    【讨论】:

      【解决方案2】:

      我知道有点晚了,但一种方法可能是为 laravel 邮件程序提供一个快速邮件程序。

      <?php
      
      $transport = (new \Swift_SmtpTransport('host', 'port'))
          ->setEncryption(null)
          ->setUsername('username')
          ->setPassword('secret');
      
      $mailer = app(\Illuminate\Mail\Mailer::class);
      $mailer->setSwiftMailer(new \Swift_Mailer($transport));
      
      $mail = $mailer
          ->to('user@laravel.com')
          ->send(new OrderShipped);
      

      【讨论】:

      • 谢谢,帮了大忙!
      • 这是在 L5.1 上对我有用的唯一方法。使用 Mail 门面不正常地恢复为默认邮件提供程序
      【解决方案3】:

      所选答案对我不起作用,我需要添加以下内容才能注册更改。

      Config::set('key', 'value');
      (new \Illuminate\Mail\MailServiceProvider(app()))->register();
      

      【讨论】:

      • 这个解决方案在我使用 Laravel 4.2 时很有帮助。我不再使用 Laravel,您可能想打开另一个线程以获得有关 Laravel 5+ 的答案。祝你好运!
      【解决方案4】:

      您可以使用Config::set 即时设置/更改任何配置:

      Config::set('key', 'value');
      

      所以,要设置/更改mail.php 中的端口,您可以试试这个:

      Config::set('mail.port', 587); // default
      

      注意:在运行时设置的配置值仅针对 当前请求,不会转移到后续请求。 Read more.

      更新A hack for saving the config at runtime.

      【讨论】:

      • 谢谢!正是我想要的
      • 我面临着类似的问题。我创建了一个服务提供者MailServiceProvider 并在register() 函数中像这样声明Config::set('mail.port', 587); Config::set('mail.driver', smtp)。但这似乎不起作用。帮助赞赏:) 原始问题:link
      • 您不应该像那样在运行时更改配置设置。请改用数据库。
      • @Saurabh Config::set() 在 boot() 中工作,至少对我而言
      • 你需要这个use Illuminate\Support\Facades\Config;
      猜你喜欢
      • 2018-05-20
      • 2019-03-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多