如何在使用Laravel在控制器中发送邮件之前更改邮件配置?

时间:2014-05-02 23:15:36

标签: php email laravel

我正在使用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');

感谢您的支持!

4 个答案:

答案 0 :(得分:18)

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

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

因此,要设置/更改mail.php中的端口,您可以尝试这样做:

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

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

更新A hack for saving the config at runtime.

答案 1 :(得分:2)

所选答案对我没有用,我需要为要注册的更改添加以下内容。

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

答案 2 :(得分:1)

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

解决方案:

  1. Laravel 模型:首先,您需要创建一个数据库表来存储用户的电子邮件配置数据。接下来,您需要一个 Eloquent 模型来检索经过身份验证的用户的 ID,以动态获取他们的电子邮件配置数据。
  2. Laravel ServiceProvider:接下来,创建一个服务提供者,该服务提供者将使用 Model 类中的范围方法查询数据库中用户的电子邮件配置,并将其设置为他们的默认邮件配置。请勿在您的 config/app.php
  3. 中注册此服务提供商
  4. 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);
  }
}

既然我们已经实现了所有这些,请在 $routedMiddlewarekennel.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

答案 3 :(得分:0)

我知道有点晚了,但是一种方法可能是向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);
相关问题