无法通过单次发送向 SendGrid 中的特定联系人 list_ids 发送批量电子邮件,出现“Json 无法解组”错误

问题描述 投票:0回答:1

我正在使用 SendGrid 的新营销活动 API 发送单次发送(向特定联系人列表发送电子邮件)。当我尝试使用“send_at: now”参数创建单次发送时,我收到 400 响应和此错误消息:

{"errors":[{"field":"","message":"json could not be unmarshalled"}]}. 

但是,当我创建不带“send_at: now”参数的单次发送时,它会被创建为草稿,因为未指定时间,并且我收到 201 响应。

$sg = new \SendGrid($_ENV['SENDGRID_API_KEY']);
$list_ids = '"' . implode('","', $list_ids) . '"';

$request_body = json_decode('{
    "name": "'.$name.'",
    "send_at": "now",
    "send_to":{"list_ids":["222418c1-4360-47ed-xxxx-xxxxxxxx"]},
    "email_config":{"subject":"hello","html_content":"hi there"}
}');

try {
    $response = $sg->client->marketing()->singlesends()->post($request_body);
    echo "<br>"."<br>"."<br>";
    echo '<pre>';
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
    echo '</pre>';
    return true;
} catch (Exception $ex) {
    echo 'Caught exception: ' .  $ex->getMessage();
    return false;
}

所以我面临的问题是:

  1. 当我尝试使用 SendGrid 的营销活动 API 和

    send_at : "now"
    参数发送单次发送时,我收到一条错误消息,显示
    {"errors":[{"field":"","message":"json could not be unmarshalled"}]}
    。我不确定为什么会出现此错误以及如何修复它。

  2. 如果我在不使用

    send_at : "now"
    参数的情况下创建单个发送,则会将单个发送创建为草稿。但是,我需要
    schedule this Single Send email campaign
    时间,因为在创建单次发送期间未提供该时间。

  3. 当我尝试安排单次发送时,收到一条错误消息,要求我在 JSON 中插入 custom_unsubscribe URL

    $request_body
    。我不确定如何插入此 URL,而且我也很困惑为什么 SendGrid 在未提供的情况下无法自动插入取消订阅链接。 (我不想使用 custom_unsubscribe URL,默认取消订阅 URL 对我来说完全没问题。

php sendgrid
1个回答
0
投票

您似乎正在尝试使用旧版本的 Schedule Single Send API 或 PHP Helper Library。

当前版本的API仅支持请求体中的

send_at
参数。此外,方法调用结构与您正在使用的略有不同,正如您在下面的示例中看到的,从文档复制(并稍作修改)。

<?php

require_once 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required(['SENDGRID_API_KEY']);

$sg = new \SendGrid($_ENV['SENDGRID_API_KEY']);
$request_body = json_decode('{
    "send_at": "3752-01-28T23:21:52.575Z"
}');
$id = "ZGkrHSypTsudrGkmdpJJ";

try {
    $response = $sg
        ->client
        ->marketing()
        ->singlesends()
        ->_($id)
        ->schedule()
        ->put($request_body);
    print $response->statusCode() . "\n";
} catch (Exception $ex) {
    echo 'Caught exception: '.  $ex->getMessage();
}
© www.soinside.com 2019 - 2024. All rights reserved.