0

I try to send transactionnal emails with attributes but the Sendinblue API's return a

400 Bad Request {"code":"invalid_parameter","message":"attributes are not valid"}

In the Sendinblue documentation, they say to use a json object and the set method's too.

/**
     * 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;
    }

Below, my code to send an email, I use their example:

$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;
}

I have had my attributes in my mail template (like %FNAME%).

If I don't include attributes, it works.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Tristan_CH
  • 31
  • 1
  • 4
  • I am sure this is probably nothing to do with the error but could you try `$sendEmail->setAttributes('{"FNAME":"John", "LNAME":"Doe"}');` – RiggsFolly Oct 28 '19 at 16:17
  • Are the attributes correct? They have an endpoint you can query to get all attributes; https://api.sendinblue.com/v3/contacts/attributes – IsThisJavascript Oct 28 '19 at 16:18
  • @RiggsFolly I would like but unfortunately no – Tristan_CH Oct 28 '19 at 16:21
  • @IsThisJavascript Yes they are correct ans I just use transactionnal email with no contact – Tristan_CH Oct 28 '19 at 16:23
  • 1
    I have come back from a google-fu session with hopefully some relevant information for you. Whilst digging into `->setAttributes` I came across this fellow who has his setup (and working) like so: `->setAttributes(["USERNAME" => $user->getUsername(), "LINK" => $link]);` – IsThisJavascript Oct 28 '19 at 16:38
  • 1
    Sooo... May you try; `->setAttributes(["FNAME" => "John", "LNAME" => "Doe"]);` – IsThisJavascript Oct 28 '19 at 16:39
  • 1
    @IsThisJavascript Yes thank you, **it works** !!! I have asked at sendinblue why a json don't work, I will publish the answer. – Tristan_CH Oct 28 '19 at 17:01

1 Answers1

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;
}
?>

Please try this code. You can find example at https://developers.sendinblue.com/reference#sendtemplate-1