0

我尝试发送带有属性的事务性电子邮件,但 Sendinblue API 返回一个

400 错误请求 {"code":"invalid_parameter","message":"attributes are not valid"}

在 Sendinblue 文档中,他们说要使用json 对象和 set 方法。

/**
     * Sets attributes
     *
     * @param object $attributes Pass the set of attributes to customize the template. For example, {\"FNAME\":\"Joe\", \"LNAME\":\"Doe\"}
     *
     * @return $this
     */
    public function setAttributes($attributes)
    {
        $this->container['attributes'] = $attributes;

        return $this;
    }

下面,我发送电子邮件的代码,我使用他们的例子:

$templateId = 2; // int | Id of the template
$sendEmail = new \SendinBlue\Client\Model\SendEmail(); // \SendinBlue\Client\Model\SendEmail
$sendEmail->setEmailTo(array('example@mail.com')); //for stackoverflow
$sendEmail->setAttributes('{"LNAME":"John","FNAME":"Doe"}');

try {
    $result = $apiInstance->sendTemplate($templateId, $sendEmail);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling AccountApi->getAccount: ', $e->getMessage(), PHP_EOL;
}

我的邮件模板中有我的属性(如%FNAME%)。

如果我不包含属性,它可以工作。

4

1 回答 1

0
<?php
require_once(__DIR__ . '/vendor/autoload.php');

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

$apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(
    new GuzzleHttp\Client(),
    $config
);
$templateId = 1;
$sendEmail = new \SendinBlue\Client\Model\SendEmail()
$sendEmail['emailTo'] = array('example@example.com');
$sendEmail['emailCc'] = array('example1@example1.com');
$sendEmail['headers'] = array('Some-Custom-Name' => 'unique-id-1234');
$sendEmail['attributes'] = array('FNAME' => 'Jane', 'LNAME' => 'Doe');
$sendEmail['replyTo'] = 'replyto@domain.com';
$sendEmail['attachmentUrl'] = 'https://example.net/upload-file.pdf';

try {
    $result = $apiInstance->sendTemplate($templateId, $sendEmail);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling TransactionalEmailsApi->sendTemplate: ', $e->getMessage(), PHP_EOL;
}
?>

请尝试此代码。您可以在https://developers.sendinblue.com/reference#sendtemplate-1找到示例

于 2020-11-01T12:55:29.877 回答