You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Livewire控制器配置SMTP批量发件时出现类型错误

解决Livewire动态配置SMTP批量发邮件的类型错误

问题场景

在Livewire控制器中动态配置SMTP并通过foreach循环批量发送邮件时,触发以下错误:

Illuminate\Mail\MailManager::getConfig(): Argument #1 ($name) must be of type string, null given

错误触发路径:/Users/msurubel/Web_Application_Projects/Lourent/Email_Marketing_App/main_app/vendor/laravel/framework/src/Illuminate/Mail/MailManager.php第107行

控制器原代码:

$getAllSubscribers = subscribers::wheregroup_id($this->SelectingGroupInput)->get();
//Config SMTP
$getSMTPConfig = smtp_configs::whereref_id($this->SelectingSMTPConfigInput)->first();
$mailConfig = array(
    'mailer'     => $getSMTPConfig->mailler,
    'host'       => $getSMTPConfig->host,
    'port'       => $getSMTPConfig->port,
    'from'       => array('address' => $this->SendingFromEmail, 'name' => $this->SendingFromName),
    'encryption' => $getSMTPConfig->encryption,
    'username'   => $getSMTPConfig->username,
    'password'   => $getSMTPConfig->password,
);
Config::set('mail', $mailConfig);

foreach ($getAllSubscribers as $lists){
  $getSubscriberData = subscribers::whereid($lists->id)->first();

  $details = [
      'CompanyName' => $this->SendingFromName,
      'subject' => $this->SendingSubjectline,
      'MailBody' => "Hi, How are you.",
      'name' => "App",
  ];
  Mail::to("$getSubscriberData->email")->send(new BulkMail($details));
}

错误原因

  1. 配置结构错误:直接用Config::set('mail', $mailConfig)覆盖整个邮件配置,破坏了Laravel默认的mail配置结构(原结构包含mailers数组、default字段等),导致MailManager无法找到正确的配置项,传入getConfig的参数为null。
  2. 拼写错误:$getSMTPConfig->mailler应为$getSMTPConfig->mailer(多写了一个字母i),会导致驱动名称无效。
  3. 冗余查询:循环内重复查询subscribers模型,完全可以直接使用集合中已有的模型数据,浪费数据库资源。

修复方案

1. 修正SMTP配置结构

不要覆盖整个mail配置,而是在mail.mailers下新增/更新自定义驱动,并设置默认邮件驱动为该自定义驱动,同时保留原配置的必要部分。

2. 修正拼写错误

将mailler改为mailer。

3. 优化循环查询

直接使用循环变量$lists获取邮箱地址,无需重复查询数据库。

修改后的完整代码

$getAllSubscribers = subscribers::wheregroup_id($this->SelectingGroupInput)->get();
// 获取SMTP配置
$getSMTPConfig = smtp_configs::whereref_id($this->SelectingSMTPConfigInput)->first();

// 定义自定义邮件驱动名称(可自定义,比如'smtp_custom')
$customMailerName = 'smtp_custom';

// 构建正确的驱动配置,合并到原mailers配置中
$mailerConfig = [
    'transport' => 'smtp',
    'host'       => $getSMTPConfig->host,
    'port'       => $getSMTPConfig->port,
    'encryption' => $getSMTPConfig->encryption,
    'username'   => $getSMTPConfig->username,
    'password'   => $getSMTPConfig->password,
    'timeout'    => null,
    'auth_mode'  => null,
];

// 更新mailers配置
Config::set("mail.mailers.$customMailerName", $mailerConfig);
// 设置默认邮件驱动为自定义驱动
Config::set('mail.default', $customMailerName);
// 设置全局发件人信息
Config::set('mail.from', [
    'address' => $this->SendingFromEmail,
    'name' => $this->SendingFromName
]);

foreach ($getAllSubscribers as $list){
    $details = [
        'CompanyName' => $this->SendingFromName,
        'subject' => $this->SendingSubjectline,
        'MailBody' => "Hi, How are you.",
        'name' => "App",
    ];
    Mail::to($list->email)->send(new BulkMail($details));
}

内容的提问来源于stack exchange,提问作者ZDeveloper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 23:50:30