I am trying to send emails to multiple recipients using SendinBlue's API v3 Php Library (https://github.com/sendinblue/APIv3-php-library).
The following code, I think, it set up correctly - in that the code goes down printing the $result
part of the code, meaning there was no exception.
However, when testing sending to multiple recipients, only the email address that's the last one in the array ([email protected] in the code below) receives the email.
If I flip the to
array contents around, so that [email protected]
appears last in the array, then only that address receives the email.
This is the sample code:
// ####################################################
// Sendinblue Email
// ####################################################
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', $send_in_blue_api_key);
$apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(
new GuzzleHttp\Client(),
$config
);
$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail();
$sendSmtpEmail['subject'] = 'Mulitple Recipients Email Test';
$sendSmtpEmail['htmlContent'] = $html;
$sendSmtpEmail['sender'] = array('name' => 'Test Messages', 'email' => '[email protected]');
$sendSmtpEmail['to'] = array(
array('email' => '[email protected]', 'name' => 'Bugs Bunny'
, 'email' => '[email protected]', 'name' => 'Daffy Duck')
);
$sendSmtpEmail['replyTo'] = array('email' => '[email protected]', 'name' => 'Reply Name');
try {
$result = $apiInstance->sendTransacEmail($sendSmtpEmail);
print_r($result);
} catch (Exception $e) {
$send_error = $e->getMessage();
print_r($send_error);
}
I tried changing the to
array from:
$sendSmtpEmail['to'] = array(
array('email' => '[email protected]', 'name' => 'Bugs Bunny'
, 'email' => '[email protected]', 'name' => 'Daffy Duck')
);
To:
$sendSmtpEmail['to'] = array('email' => '[email protected]', 'name' => 'Bugs Bunny'
, 'email' => '[email protected]', 'name' => 'Daffy Duck');
But, the API returns this, which I think means the way I'm defining the multiple recipients in the to
array is correct:
[400] Client error: `POST https://api.sendinblue.com/v3/smtp/email` resulted in a `400 Bad Request` response:
{"code":"invalid_parameter","message":"to is not valid"}
I wondered if there is any way around this issue?
CodePudding user response:
You have to make an array of arrays. Each array should have email
and name
key:
Short array syntax:
$sendSmtpEmail['to'] = [
['email' => '[email protected]', 'name' => 'Bugs Bunny'],
['email' => '[email protected]', 'name' => 'Daffy Duck'],
];
Equivalent to:
$sendSmtpEmail['to'] = array(
array('email' => '[email protected]', 'name' => 'Bugs Bunny'),
array('email' => '[email protected]', 'name' => 'Daffy Duck'),
);