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

Zeptomail API带附件请求格式错误(TM_3301/SM_101)求助

修复Zeptomail API请求格式错误(TM_3301/SM_101)

问题根源

  • 变量未解析:单引号包裹的JSON字符串里直接用$base64_attFile变量,PHP不会解析单引号内的变量,导致API收到的是字符串"$base64_attFile"而非实际的base64编码内容。
  • Attachments格式错误:Zeptomail API要求attachments字段是数组类型,你写成了单个对象,不符合接口规范。
  • 手动拼接JSON易出错:手动编写JSON字符串容易出现语法问题(比如引号未转义、格式不严谨),进而触发语法错误。

修复后的代码

<?php
$file = '169242759486.pdf';
$attFile = 'https://www.example.com/secureQCReports/' . $file;

// 获取远程文件内容并转base64(注意:需确保php.ini中allow_url_fopen=On)
$fileContent = file_get_contents($attFile);
if (!$fileContent) {
    die("无法获取远程文件");
}
$base64_attFile = chunk_split(base64_encode($fileContent));

// 构建请求数据数组,避免手动拼接JSON
$postData = [
    "from" => ["address" => "admin@udyog.one", "name" => "Ugyod One"],
    "to" => [["email_address" => ["address" => "nkbarman@hotmail.com", "name" => "Nimesh"]]],
    "reply_to" => ["address" => "sangita143@gmail.com", "name" => "Sangita Barman"],
    "subject" => "Test Email",
    "htmlbody" => "<div><b> Test email sent successfully. </b></div>",
    "attachments" => [ // 改为数组格式
        [
            "content" => $base64_attFile,
            "mime_type" => "application/pdf",
            "name" => "myReport.pdf"
        ]
    ]
];

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.zeptomail.in/v1.1/email",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($postData), // 用json_encode生成标准JSON
    CURLOPT_HTTPHEADER => array(
        "accept: application/json",
        "authorization: Zoho-enczapikey **************",
        "cache-control: no-cache",
        "content-type: application/json",
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}
?>

关键修改说明

  1. 用数组+json_encode构建请求体:避免手动拼接JSON的语法错误,确保变量被正确解析。
  2. Attachments改为数组:将单个对象包裹在数组中,符合Zeptomail API的参数要求。
  3. 增加文件获取校验:添加file_get_contents的结果判断,提前排查文件获取失败的问题。
  4. 变量解析正常:数组中的$base64_attFile会被PHP正确替换为实际的base64内容,再由json_encode转为标准JSON字符串。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 00:33:33