改变 Laravel 邮件配置在运行时

huangapple go评论83阅读模式
英文:

Change Laravel mail configuration during runtime

问题

我需要在运行时更改Laravel的邮件配置。这是因为在运行时需要多次更改配置,而不仅仅在应用初始化时进行一次更改。

foreach ($emails as $email) {
    Config::set('mail.mailers.smtp.username', $email->email);
    Config::set('mail.mailers.smtp.password', $email->password);
    Config::set('mail.from.address', $email->email);

    Mail::to('someone@example.com')->send(new DemoMail('title', 'body'));
}

正如您所看到的,我正在尝试在每次迭代中设置新的配置,但配置仅在第一次迭代时设置;所有其他电子邮件都使用第一次迭代的设计发送。

我已经找到了使用服务提供程序的解决方案。类似于此https://stackoverflow.com/questions/45146260/dynamic-mail-configuration-with-values-from-database-laravel
问题在于提供程序在应用初始化时设置邮件配置,无法在运行时更改。因此,这不能解决我的问题。

我想到的第二个想法是定义多个邮件驱动程序(配置),每个邮件一个,然后在每次迭代中使用mailer()函数指定要使用哪一个。这个解决方案的问题在于我需要动态创建这些驱动程序;我不能硬编码它们。

我在互联网上搜索了正确的解决方案,但找不到一个。

英文:

I need to change Laravel Mail Configuration during the runtime. This is because the configuration needs to be changed several times during runtime, not just once on app initialization.

foreach ($emails as $email) {
    Config::set('mail.mailers.smtp.username', $email->email);
    Config::set('mail.mailers.smtp.password', $email->password);
    Config::set('mail.from.address', $email->email);

    Mail::to('someone@example.com')->send(new DemoMail('title', 'body'));
}

As you can see, I am trying to set new configurations on each iteration, but the configuration is only set once (on the first iteration); all other emails are sent using the design from the first iteration.

I have found solutions using Service Providers. Similar to this one https://stackoverflow.com/questions/45146260/dynamic-mail-configuration-with-values-from-database-laravel
The problem is that the provider sets mail configuration when the app is initialized and can't be changed during runtime. So this doesn't solve my problem.

The second idea I came up with is to define multiple mail drivers (configurations), one for each email, and then use the mailer() function to specify which one I want to use on each iteration. The problem with this solution is that I need to create those drivers dynamically; I can't hardcode them.

I searched the internet for the right solution, but couldn't find one.

答案1

得分: 1

我已使用Symfony Mailer在Laravel中找到了解决方案。

foreach ($emails as $email) {
    $transport = new EsmtpTransport('mail.host', 465);
    $transport->setUsername($email->email);
    $transport->setPassword($email->password);

    $mailer = new Mailer($transport);

    $mailable = (new SymfonyEmail())->from($email->email)->bcc('test@test.com')->subject($request->subject)->html('email body');

    $mailer->send($mailable);
}
英文:

I have found a solution by using Symfony Mailer in Laravel.

foreach ($emails as $email) {
    		$transport = new EsmtpTransport('mail.host', 465);
    		$transport->setUsername($email->email);
    		$transport->setPassword($email->password);

    		$mailer = new Mailer($transport);

    		$mailable = (new SymfonyEmail())->from($email->email)->bcc('test@test.com')->subject($request->subject)->html('email body');

   			$mailer->send($mailable);
    	} 

答案2

得分: 0

@itstare也许这个Laravel News可以帮助你找到处理的方式。

参考讨论来自于这个Laracast

英文:

@itstare maybe this Laravel News can help you to find the way to work with.

the reference discussion is from this Laracast

huangapple
  • 本文由 发表于 2023年4月6日 21:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定