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 (person2@exampe.com in the code below) receives the email.
If I flip the to
array contents around, so that person1@example.com
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' => 'messages@example.com');
$sendSmtpEmail['to'] = array(
array('email' => 'person1@example.com', 'name' => 'Bugs Bunny'
, 'email' => 'person2@example.com', 'name' => 'Daffy Duck')
);
$sendSmtpEmail['replyTo'] = array('email' => 'sender@example.com', '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' => 'person1@example.com', 'name' => 'Bugs Bunny'
, 'email' => 'person2@example.com', 'name' => 'Daffy Duck')
);
To:
$sendSmtpEmail['to'] = array('email' => 'person1@example.com', 'name' => 'Bugs Bunny'
, 'email' => 'person2@example.com', '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?
You have to make an array of arrays. Each array should have email
and name
key:
Short array syntax:
$sendSmtpEmail['to'] = [
['email' => 'person1@example.com', 'name' => 'Bugs Bunny'],
['email' => 'person2@example.com', 'name' => 'Daffy Duck'],
];
Equivalent to:
$sendSmtpEmail['to'] = array(
array('email' => 'person1@example.com', 'name' => 'Bugs Bunny'),
array('email' => 'person2@example.com', 'name' => 'Daffy Duck'),
);