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

SendinBlue PHP API v3发送多收件人仅送达最后一个的问题

解决SendinBlue API v3 PHP库多收件人仅最后一个收到邮件的问题

你的问题出在to数组的结构上——你把两个收件人的信息塞在了同一个子数组里,而PHP中数组的键名是唯一的,重复的键会被最后一个值直接覆盖。比如你原来的写法:

$sendSmtpEmail['to'] = array(
    array('email' => 'person1@example.com', 'name' => 'Bugs Bunny'
        , 'email' => 'person2@example.com', 'name' => 'Daffy Duck')
);

这个子数组里有重复的email和name键,PHP会自动丢弃第一组值,只保留最后一组,所以API实际只收到了person2@example.com这一个收件人,自然只有最后一个地址能收到邮件。

修正方案

正确的多收件人格式是每个收件人对应一个独立的子数组,把它们放在外层数组中,修改后的to部分代码如下:

$sendSmtpEmail['to'] = array(
    array('email' => 'person1@example.com', 'name' => 'Bugs Bunny'),
    array('email' => 'person2@example.com', 'name' => 'Daffy Duck')
);

修正后的完整代码

// ####################################################
// Sendinblue Email
// ####################################################

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', $send_in_blue_api_key);

$apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(
    new GuzzleHttp\Client(),
    $config
);
$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail();
$sendSmtpEmail['subject'] = 'Mulitple Recipients Email Test';
$sendSmtpEmail['htmlContent'] = $html;
$sendSmtpEmail['sender'] = array('name' => 'Test Messages', 'email' => 'messages@example.com');
// 修正后的多收件人结构
$sendSmtpEmail['to'] = array(
    array('email' => 'person1@example.com', 'name' => 'Bugs Bunny'),
    array('email' => 'person2@example.com', 'name' => 'Daffy Duck')
);
$sendSmtpEmail['replyTo'] = array('email' => 'sender@example.com', 'name' => 'Reply Name');
try {
    $result = $apiInstance->sendTransacEmail($sendSmtpEmail);
    print_r($result);
} catch (Exception $e) {
    $send_error = $e->getMessage();
    print_r($send_error);
}

补充说明

  • 你之前改成单层数组的写法返回400错误,是因为SendinBlue API要求to必须是数组的数组(每个元素是包含收件人信息的独立数组),单层数组不符合参数格式要求。
  • 最初的写法虽然是二维数组,但内部子数组的键重复导致数据丢失,API只处理了最后一个收件人,所以没有抛出参数错误,但实际只发送了一封邮件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:15:49