无法弄清楚将模板ID放入mailjet API调用

时间:2019-04-11 11:31:36

标签: php mailjet

我正在使用mailjet API发送广告系列。目前,我执行此操作的功能如下:

public function sendCampaign($listName, $config, $vars) {
    //First we need to set up a draft of the campaign.
    $listId = $this->config['lists'][$listName];
    $title = $config['subject'] . "_" . time();
    $requestBodyOne = [
        'Locale' => 'de_DE',
        'Sender' => $config['fromName'],
        'SenderEmail' => $config['fromEmail'],
        'Subject' => $config['subject'],
        'ContactsListID' => $listId,
        'TemplateID' => $config['template'],                                //TODO: Figure out how to get templating to work -- right now, this is being ignored!
        'Title' => $title,
    ];
    $responseOne = $this->client->post(Resources::$Campaigndraft, ['body' => $requestBodyOne]);

    if(!$responseOne->success()) {
        $responseOne->getStatus();
    }

    $data = $responseOne->getData();
    $draftId = $data[0]['ID'];

    //Now we add our body to the draft
    $requestBodyTwo = [
        'Html-part' => $vars['body'],
    ];
    $responseTwo = $this->client->post(Resources::$CampaigndraftDetailcontent, ['id' => $draftId, 'body' => $requestBodyTwo]);
    if(!$responseTwo->success()) {
        $responseTwo->getStatus();
    }

    //Now we can send the campaign
    $responseThree = $this->client->post(Resources::$CampaigndraftSend, [
        'id' => $draftId,
        'TemplateID' => $config['template']                                 //TODO: Figure out how to get templating to work -- right now, this is being ignored!
    ]);

    die(dump($responseThree));
}

(我意识到这里有一些气味-一旦使大功能起作用,我会将其分解为较小的功能。)

尽管我在两个位置设置了TemplateID,但似乎无法让mailjet使用该模板(该模板接受一个“ body”变量)。相反,我的消息以HTML格式发送,没有包装模板。我已确认我的模板存在。

我在这里可能做错了什么?

====

编辑:更改hte请求,使其看起来像这样:

    $requestBodyTwo = [
        'Vars' => ['body' => $vars['body']]
    ];

...导致400错误:“ JSON有效负载中不支持属性”

1 个答案:

答案 0 :(得分:0)

在此结束循环:我们最终使用了/ send端点并输入了MJ-campaign的值。做到了。

https://dev.mailjet.com/reference/email/send-emails/