我正在尝试动态更改 Web 或队列的 SMTP 详细信息。
我已经尝试过此链接:https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel,但此示例在 Laravel 中不起作用10.
不仅如此,我还尝试了这段代码:
$configuration = $this->configuration();
$customer = Customer::find(1);
$notification['message'] = "Message";
$mailer = new CustomerMail($customer, $notification['message'], $configuration['smtp_from_email'], $configuration['smtp_from_name']);
$mailer->withSwiftMessage(function ($message) use ($configuration) {
$message->getHeaders()
->addTextHeader('X-SMTP-Transport', $configuration['smtp_transpot'])
->addTextHeader('X-SMTP-Host', $configuration['smtp_host'])
->addTextHeader('X-SMTP-Port', $configuration['smtp_port'])
->addTextHeader('X-SMTP-Encryption', $configuration['smtp_encryption'])
->addTextHeader('X-SMTP-Username', $configuration['smtp_username'])
->addTextHeader('X-SMTP-Password', $configuration['smtp_password']);
});
Mail::to($customer->{Customer::EMAIL})->send($mailer);
此代码也不适合我,如何在 Laravel 10 中动态更改 SMTP 详细信息而不影响配置值。
Laravel10 使用 Symfony Mailer 而不是 SwiftMailer。 https://laravel.com/docs/9.x/upgrade#symfony-mailer
配置覆盖是临时的,不会保存。
config(['mail.mailers.smtp.username' => '']);
config(['mail.mailers.smtp.password' => '']);
// sendmail
这只在当前请求期间生效。