首页 > 解决方案 > 如何在 Sendinblue api v3 中设置交易电子邮件属性?

问题描述

所以,我充其量是 php 的新手,但我已经知道如何使用 sendinblue 设置和发送交易电子邮件。

但无论出于何种原因,我似乎无法设置属性。

这确实是我似乎无法开始工作的唯一代码行。

$sendEmail['attributes'] = array('FIRSTNAME' => "STEVE");$sendEmail['attributes'] = array('FIRSTNAME' => "STEVE"); 我也试过

$sendEmail['params'] = array('FIRSTNAME' => "STEVE");

$params['attributes'] = array('FIRSTNAME' => "STEVE");

...可能还有上述的 127 种变体,但我似乎无法让它发挥作用。

我似乎也无法弄清楚如何使用 php 创建联系人...

这行代码的“创建联系人”等价物是什么:

$sendEmail = new \SendinBlue\Client\Model\SendEmail();

?

就像我说的,我的电子邮件正在发送,但我希望他们阅读“亲爱的史蒂夫”,他们阅读“亲爱的”

以下是完整代码:

<?php


# Include the SendinBlue library\
require_once('../vendor/autoload.php');

# Instantiate the client\

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'MY API KEY HERE');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('api-key', 'Bearer');
// Configure API key authorization: partner-key
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('partner-key', 'MY API KEY HERE');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('partner-key', 'Bearer');

$apiInstance = new SendinBlue\Client\Api\SMTPApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);


$templateId = 2; // int | Id of the template

$sendEmail = new \SendinBlue\Client\Model\SendEmail(); // \SendinBlue\Client\Model\SendEmail | 

 $sendEmail['emailTo'] = array("test@example.com");

$params['attributes'] = array('FIRSTNAME' => "STEVE"); //THIS IS THE LINE OF CODE THAT ISN'T WORKING.


//$mail->setFrom('info@myeasy.wedding', 'My Easy Wedding');



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

?>

标签: phpsendinblue

解决方案


您需要使用sendTransacEmail()它,它与 PARAM 一起使用。需要在模板中添加代码{{ params.FIRSTNAME }}

完整的 API 代码

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'MY API KEY HERE');
$apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);


$sendEmail = new SendinBlue\Client\Model\SendSmtpEmail();
$sendEmail['to'] = [["email" => 'test@example.com', "name" => 'test']];
$sendEmail['templateId'] = 2;
$sendEmail['params'] = ['FIRSTNAME' => 'Test'];



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

推荐阅读