SparkPost发送电子邮件模板

时间:2015-11-19 09:40:31

标签: php email sparkpost

我正在使用 SparkPost PHP API 向收件人发送电子邮件,但我遇到此错误。目前我已经设置了所有必要的领域,但我仍然遇到大型电子邮件的问题。我可以轻松发送小文本,但我对大数据有困难。

  

[{“message”:“缺少必填字段”,“说明”:“'内容'中至少需要存在'text'或'html'之一”,“code”:“1400”}]

我的代码是:

for ($j = 0; $j < count($result1arr); $j++) {

    try {

        SparkPost::setConfig(["key" => "XXXXX"]);
        $results = Transmission::send(array(
                    "from" => "test@universityfood.co",
                    "html" => $resultarr['mailBody'],
                    "text" => $resultarr['mailBody'],
                    "subject" => $resultarr['subject'],
                    "recipientList" => $result1arr[$j]['groupName']
        ));

        $_SESSION['success_message'] = 'Email sended successfully to Recipient List with ID : ' . $data['recipients_id'];
        $qry = "DELETE  from mailQueue where mailQueueId={$result1arr[$j]['mailQueueId']}";
        $res = $conn->query($qry);
        $sql1 = "INSERT INTO sendMailHistory (schoolName,noOfMailSent) VALUES ('{$result1arr[$j]['originalGroupId']}','{$results['results']['total_accepted_recipients']}')";
        $result1 = $conn->query($sql1);

        $chc = curl_init();
        curl_setopt($chc, CURLOPT_URL, "https://api.sparkpost.com/api/v1/recipient-lists/{$result1arr[$j]['groupName']}");
        curl_setopt($chc, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($chc, CURLOPT_HEADER, FALSE);
        curl_setopt($chc, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($chc, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_setopt($chc, CURLOPT_HTTPHEADER, array(
            "Accept: application/json",
            "Authorization: XXXXX"
        ));

        $response = curl_exec($chc);
        curl_close($chc);
        header('Location: ../index.php');
        exit;
        return true;
    } catch (\Exception $exception) {
        echo $exception->getMessage();
    }
}

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为$resultarr['mailBody']未定义。

您确定阵列$resultarr存在吗?如果是这样,则必须缺少mailBody元素。

您可以提供text部分或html部分,或两者兼而有之。但如果您不提供,则会收到错误:At least one of 'text' or 'html' needs to exist in 'content'

在这种情况下,两者都没有提供,因为未定义$resultarr['mailBody']会导致数组的texthtml元素未定义。