Sendinblue PHP API - multiple recipients only sends to last in array - php

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'),
);

Related

Bulk sending to multiple recipients docusign php

Good morning, I'm trying to send a document to multiple recipients, but it's only sending to one, and it doesn't generate any error!
$signers = [];
# Create the signer recipient model
$signer1 = new \DocuSign\eSign\Model\Signer([
'email' => $this->email_signatario, 'name' => $this->nome_signatario,
'recipient_id' => "1", 'routing_order' => "5"]);
array_push( $signers, $signer1 );
$routing_order_count = 6;
foreach ( $this->emails_adicionais as $endereco_adicional ) {
$signer = new \DocuSign\eSign\Model\Signer([
'email' => $endereco_adicional, 'name' => $this->nome_signatario . "_$routing_order_count",
'recipient_id' => $routing_order_count, 'routing_order' => $routing_order_count]);
array_push( $signers, $signer );
$routing_order_count++;
}$sign_here1 = new \DocuSign\eSign\Model\SignHere([
'anchor_string' => '*signature_1*', 'anchor_units' => 'pixels',
'anchor_y_offset' => '10', 'anchor_x_offset' => '20']);
$sign_here2 = new \DocuSign\eSign\Model\SignHere([
'anchor_string' => '/sn1/', 'anchor_units' => 'pixels',
'anchor_y_offset' => '10', 'anchor_x_offset' => '20']);
# Add the tabs model (including the sign_here tabs) to the signer
# The Tabs object wants arrays of the different field/tab types
foreach ( $signers as $signer ) {
$signer->setTabs( new \DocuSign\eSign\Model\Tabs([
'sign_here_tabs' => [$sign_here1, $sign_here2]]));
}
# Add the recipients to the envelope object
$recipients = new \DocuSign\eSign\Model\Recipients([
'signers' => $signers, 'recipient_count' => count( $signers ), 'carbon_copies' => [$cc1,$cc2,$cc3,$cc4]]);
$envelope_definition->setRecipients($recipients);
This is the code I use to add signers and copies.
If anyone has an example of multiple submissions, please send them, thank you!
You have this code:
$routing_order_count++;
So you are generating recipients with routing order 1,2,3,4 etc.
The first one will get it right away, but the second one will only get it after the first one completes to sign it.
So what you see is expected.
If you want them to all get it at once - change it to :
$routing_order_count = 1

Issue when requesting create segments API v3 through PHP wrapper

I have test-driving for Mailchimp API v3 using your PHP wrapper. It's working great for me But when I'm creating a request using POST for "Create Segment" getting an error (attach screenshot):
Request Code is (through associative array) -
$api_key = "xxxxxxxxxxxxxxxx-us11";
$list_id = "1xx2xx3xx4xx";
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post('lists/' . $list_id . '/segments', array('name' => 'Testing Data',
'options' => array('match' => 'all',
'conditions' => array('field' => 'type', 'op' => 'is', 'value' => 'Testing'))
));
This request call returning following error -
array (size=2) 'field' => string 'options.conditions' (length=18)
'message' => string 'Schema describes array, object found instead'
(length=44)
I will also tried to create Request (through associative array) -
Method 1:
$api_key = "xxxxxxxxxxxxxxxx-us11";
$list_id = "1xx2xx3xx4xx";
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post('lists/' . $list_id . '/segments', array('name' => 'Testing Data',
'options' => array('match' => 'all',
'conditions' => array(array('field' => 'type', 'op' => 'is', 'value' => 'Testing')))
));
Method 2:
$api_key = "xxxxxxxxxxxxxxxx-us11";
$list_id = "1xx2xx3xx4xx";
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post('lists/' . $list_id . '/segments', array('name' => 'Testing Data 4',
'options' => array('match' => 'all',
'conditions' => array(array('field' => 'type'), array('op' => 'is'), array('value' => 'Testing')))
));
Both method will create segment on mailchimp account but not have any conditions. See screenshot -
How to override this problem?
You are missing the condition_type param. It should be selected from the list provided by MailChimp in the endpoint documentation.
For example, IF the field "type" from your MailChimp list is a text field you should use 'condition_type': 'TextMerge'. In this case, conditions should have the following format:
[
{
'condition_type': 'TextMerge',
'field': 'type',
'op': 'is',
'value': 'Testing'
}
]
However, MailChimp MAY have a bug in this endpoint, since TextMerge works only on the EMAIL field. I have also stumbled upon this problem recently:
Mailchimp api v3 - can't create segment based on a TEXT merge field
https://github.com/drewm/mailchimp-api/issues/160

How to create a dynamic array for Mandrill API

I want to send an email to multiple email addresses using one Mandrill API call, here is what we do to send to only one email:
<?php
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one email address:
'to' => array(
array(
'email' => $the_email,
)
),
// ...blah blah, doesn't matter
);
?>
This is OK but while we want to send the message to multiple email addresses, we must have this:
<?php
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to more than one email:
'to' => array(
array(
'email' => $email_1,
),
array(
'email' => $email_2,
),
array(
'email' => $email_3,
),
),
// ...blah blah, doesn't matter
);
?>
As you can see, we've repeated the array part, now assuming we have this array:
$subscribers_email = array(
'email_1#xxx.com',
'email_2#xxx.com',
'email_3#xxx.com'
);
How could we possibly make the mandrill code to use $subscribers_email? Of course we can write the email addresses in the mandrill code like:
<?php
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one amil address:
'to' => array(
array(
'email' => 'email_1#xxx.com',
),
array(
'email' => 'email_2#xxx.com',
),
array(
'email' => 'email_3#xxx.com',
),
),
// ...blah blah, doesn't matter
);
?>
But that's not what I want, imagine we need to make this dynamic and as a function, like:
function sendEmailToArray($subscribers_email){
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one amil address:
'to' => array(
$subscribers_email
),
// ...blah blah, doesn't matter
);
}
So I want to make that array in a dynamic way, is that possible?
You should convert your array as your requirement.
Use this code
function sendEmailToArray($subscribers_email){
//$subscribers_email = array(
// 'email_1#xxx.com',
// 'email_2#xxx.com',
// 'email_3#xxx.com'
//);
$final_subscribers_email = array();
foreach ($subscribers_email as $key => $value) {
array_push($final_subscribers_email, array("email" => $value));
}
$mandrill_message = array(
// ...blah blah, doesn't matter
// To send to only one email address:
'to' => $final_subscribers_email,
// ...blah blah, doesn't matter
);
}
You can use array_walk for this.
$subscribers_email = array(
'email_1#xxx.com',
'email_2#xxx.com',
'email_3#xxx.com'
);
sendEmailToArray($subscribers_email);
function sendEmailToArray($subscribers_email){
// Convert the array
array_walk($subscribers_email, function(&$input){
$input = array('email' => $input);
});
// Send the emails
$mandrill_message = array(
'to' => array($subscribers_email),
);
}

AWS SES on PHP error: Unable to determine service/operation name to be authorized

I am trying to send email using AWS SES using the following PHP script:
<?php
require_once("phar://aws.phar");
use Aws\Ses\SesClient;
//Open client
$client = SesClient::factory(array(
"key" => "key",
"secret" => "secret",
"region" => "region"
));
$subject = "subject";
$messageText = "text message";
$messageHtml = "<h1>formatted message</h1>";
//Send email
try{
$response = $client->sendEmail(
array(
'Source' => 'verified_email#domain.com',
'Destination' => array(
'ToAddresses' => array('an_address#domain.com')
),
'Message' => array(
'Subject' => array('Data' => $subject),
'Body' => array('Text' => array('Data' => $messageText)),
'Html' => array('Data' => $messageHtml)
)
)
);
}catch(Exception $e){
//An error happened and the email did not get sent
echo($e->getMessage());
}
?>
Whenever I run this, it goes to the catch clause and prints to the screen the message:
Unable to determine service/operation name to be authorized
This doesn't really give me any information on what's wrong and there's no documentation on the API page about this. Any ideas?

Retrieving JSON response using HTTPful library for Laravel

I am currently building a e-mail client (inbound and outbound sending) using Mandrill as the e-mail sending / inbound service and Laravel 3.x.
In order to send messages, I am using the HTTPful bundle with the Mandrill using the following code in my mail/compose POST method.
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
Link to better formatted code above: http://paste.laravel.com/m79
Now as far as I can tell from the API log, the request is correctly made (with the expected JSON) and a response of the following format is sent back:
[
{
"email": "test#test.com",
"status": "queued",
"_id": "longmessageID"
}
]
However, what I am trying to do is access the response from the request (specifically the _id attribute), which is in JSON. Now as far as I'm aware, the HTTPful class should do this automatically (using json_decode()). However, accessing:
$request->_id;
is not working and I'm not entirely sure how to get this data out (it is required so I can record this for soft-bounce, hard-bounce and rejection messages for postmaster-like functionality)
Any assistance would be appreciated.
Edit
Using the following code, results in the mail being sent but an error returned:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request[0]->status == "queued" ) {
$success = true;
}
Results in an exception being thrown: Cannot use object of type Httpful\Response as array
I must say, a huge thanks to Aiias for his assistance. I managed to fix this myself (I must have spent hours looking at this). For anyone who wants to know, the HTTPful bundle has a body array, where the response is kept. Therefore, the code below works:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request->body[0]->status == "queued" ) {
$success = true;
}
Again, huge thanks to Aiias for clearing some major confusion up for me!

Categories