Bulk sending to multiple recipients docusign php - 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

Related

Docusign Remote Signing by email in Laravel

I want to request docusign remote signing by email in laravel.
Can anyone provide me the code for this?
https://github.com/docusign/code-examples-php/blob/master/src/Services/Examples/eSignature/SigningViaEmailService.php has code that shows you how to do this.
The relevant PHP code is this:
public static function make_envelope(array $args, $clientService, $demoDocsPath, $docxFile, $pdfFile): EnvelopeDefinition
{
$envelope_definition = new EnvelopeDefinition([
'email_subject' => 'Please sign this document set'
]);
$doc1_b64 = base64_encode($clientService->createDocumentForEnvelope($args));
# read files 2 and 3 from a local directory
# The reads could raise an exception if the file is not available!
$content_bytes = file_get_contents($demoDocsPath . $docxFile);
$doc2_b64 = base64_encode($content_bytes);
$content_bytes = file_get_contents($demoDocsPath . $pdfFile);
$doc3_b64 = base64_encode($content_bytes);
$document1 = new Document([ # create the DocuSign document object
'document_base64' => $doc1_b64,
'name' => 'Order acknowledgement', # can be different from actual file name
'file_extension' => 'html', # many different document types are accepted
'document_id' => '1' # a label used to reference the doc
]);
$document2 = new Document([ # create the DocuSign document object
'document_base64' => $doc2_b64,
'name' => 'Battle Plan', # can be different from actual file name
'file_extension' => 'docx', # many different document types are accepted
'document_id' => '2' # a label used to reference the doc
]);
$document3 = new Document([ # create the DocuSign document object
'document_base64' => $doc3_b64,
'name' => 'Lorem Ipsum', # can be different from actual file name
'file_extension' => 'pdf', # many different document types are accepted
'document_id' => '3' # a label used to reference the doc
]);
# The order in the docs array determines the order in the envelope
$envelope_definition->setDocuments([$document1, $document2, $document3]);
# Create the signer recipient model
$signer1 = new Signer([
'email' => $args['signer_email'], 'name' => $args['signer_name'],
'recipient_id' => "1", 'routing_order' => "1"]);
$cc1 = new CarbonCopy([
'email' => $args['cc_email'], 'name' => $args['cc_name'],
'recipient_id' => "2", 'routing_order' => "2"]);
return SMSDeliveryService::addSignersToTheDelivery($signer1, $cc1, $envelope_definition, $args);
}

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

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

Using the PHP SDK, how do I replace my template base document when creating an envelope?

I have created a template using a base document (a customer contract) the details of which will change when I send out an envelope but the layout will remain the same. I am trying to replace the base document from the template when creating the envelope. I read from the manual that I need to call the EnvelopeDocuments:update method. Is this correct, if so how do I implement that?
Here is what I tried below that threw an error that I am unable to figure out.
**Updated code 2/1/21 for composite templates:
private function make_envelope(array $args): EnvelopeDefinition
{
// create roles for signers
$signer = new Signer([
'email' => $args['signer_email'], 'name' => $args['signer_name'],
'role_name' => "signer", 'recipient_id' => "1",
]);
# Create the company signer recipient
$companySigner = new Signer([
'email' => $args['companySigner_email'], 'name' => $args['companySigner_name'],
'role_name' => "companySigner", 'recipient_id' =>"2"
]);
# Recipients object:
$recipients_server_template = new Recipients([
'signers' => [$signer, $companySigner]]);
# Create a composite template
$comp_template1 = new CompositeTemplate([
'composite_template_id' => "1",
'server_templates' => [
new ServerTemplate([
'sequence' => "1", 'template_id' => $args['template_id']])
],
# Add the roles via an inlineTemplate
'inline_templates' => [
new InlineTemplate([
'sequence' => "1",
'recipients' => $recipients_server_template])
]
]);
# Create the pdf document that will be added to the envelope
$doc_file = 'Fiber_Connect_Customer_Agreement.pdf';
$content_bytes = file_get_contents(self::DEMO_DOCS_PATH . $doc_file);
$base64_file_content = base64_encode($content_bytes);
# Create the document model
$documentUpdated = new Document([ # create the DocuSign document object
'document_base64' => $base64_file_content,
'name' => 'Prepared Fiber Connect Customer Agreement', # can be different from actual file name
'file_extension' => 'pdf', # many different document types are accepted
'document_id' => '1' # a label used to reference the doc
# Create a composite template for the added document
$comp_template2 = new CompositeTemplate([
'composite_template_id' => "2",
# Add the recipients via an inlineTemplate
'inline_templates' => [
new InlineTemplate([
'sequence' => "2"])
],
'document' => $documentUpdated]);
# Create the envelope definition with the composited templates
$envelope_definition = new EnvelopeDefinition([
'status' => "sent",
'composite_templates' => [$comp_template1, $comp_template2]
]);
return $envelope_definition;
}
What you need to use are compositeTemplates. We have a pretty good guide on it here: https://developers.docusign.com/docs/esign-rest-api/how-to/request-signature-composite-template-embedded/
What you need to do is to create your template with the "base document" and when you're creating the envelope, you'd need to replace the base document from the serverTemplate by specifying the document field.
Let us know if you encounter any issues while getting this to work.
I was able to edit the code which is now working again if anyone is interested for the future.
/**
* Creates envelope definition using composite templates
* Parameters for the envelope: signer_email, signer_name, signer_client_id
*
* #param $args array
* #return mixed -- returns an envelope definition
*/
private function make_envelope(array $args): EnvelopeDefinition
{
// create roles for signers
$signer = new Signer([
'email' => $args['signer_email'], 'name' => $args['signer_name'],
'role_name' => "signer", 'recipient_id' => "1",
]);
# Create the company signer recipient
$companySigner = new Signer([
'email' => $args['companySigner_email'], 'name' => $args['companySigner_name'],
'role_name' => "companySigner", 'recipient_id' => "2"
]);
# Recipients object:
$recipients_server_template = new Recipients([
'signers' => [$signer, $companySigner]
]);
# Create the pdf document that will be added to the envelope
$doc_file = 'Customer_Agreement_ADOBE_TABS.pdf';
$content_bytes = file_get_contents(self::DEMO_DOCS_PATH . $doc_file);
$base64_file_content = base64_encode($content_bytes);
# Create the document model
$documentUpdated = new Document([ # create the DocuSign document object
'document_base64' => $base64_file_content,
'name' => 'Updated Prepared Fiber Connect Customer Agreement', # can be different from actual file name
'file_extension' => 'pdf', # many different document types are accepted
'document_id' => '1' # a label used to reference the doc
]);
# Create a composite template for the added document
$comp_template1 = new CompositeTemplate([
'composite_template_id' => "1",
'document' => $documentUpdated,
'server_templates' => [
new ServerTemplate([
'sequence' => "1", 'template_id' => $args['template_id']
])
],
# Add the roles via an inlineTemplate
'inline_templates' => [
new InlineTemplate([
'sequence' => "1",
'recipients' => $recipients_server_template
])
]
]);
# Create the envelope definition with the composited templates
$envelope_definition = new EnvelopeDefinition([
'status' => "sent",
'composite_templates' => [$comp_template1]
]);
return $envelope_definition;
}

Error SHARED_VIEW_USER_LACKS_PERMISSION on DocuSign\eSign\Model\RecipientViewRequest

My scenario would be this flow in my application: Register > Sign Document > Return to Finish Page.
The user register on my application and he need to sign a document to finish his registration. He is not a DocuSign user. At the moment all my tests are at the Sandbox environment.
The envelope creation works great. If I don't use the client_user_id it sends the email for signing. But I need to use the client_user_id to use the embedded signing and get the URL for next step.
When I try to to get the URL of the envelope, I receive the following error:
errorCode: SHARED_VIEW_USER_LACKS_PERMISSION
message: User lacks shared permission to envelope. Only a user with shared access to the envelope may perform the requested operation.
Here is the code I'm using on my PHP application to try to get the URL of the recent created envelope:
$envelope = $this->docusignlib->create_document_for_signing($user, $file);
$result = $this->docusignlib->get_url_document($user, $envelope['envelope_id'], $return_url);
public function create_document_for_signing($user, $file)
{
# Document
$document = new DocuSign\eSign\Model\Document([
'document_base64' => base64_encode(file_get_contents($file)),
'name' => 'Document name',
'file_extension' => 'pdf',
'document_id' => '1'
]);
# Sign Here Position
$signHere = new DocuSign\eSign\Model\SignHere([
'document_id' => '1', 'page_number' => '2', 'recipient_id' => '1',
'tab_label' => 'Sign here', 'x_position' => '100', 'y_position' => '720'
]);
# The signer object
$signer = new DocuSign\eSign\Model\Signer([
'email' => $user->user_email,
'name' => $user->user_name,
'recipient_id' => "1",
'client_user_id' => $user->user_id,
'tabs' => new DocuSign\eSign\Model\Tabs([
'sign_here_tabs' => [$signHere]
])
]);
# Next, create the top level envelope definition and populate it.
$envelopeDefinition = new DocuSign\eSign\Model\EnvelopeDefinition([
'email_subject' => "Email subject",
'documents' => [$document],
'recipients' => new DocuSign\eSign\Model\Recipients(['signers' => [$signer]]),
'status' => "sent"
]);
$config = new DocuSign\eSign\Configuration();
$config->setHost($this->api);
$config->addDefaultHeader("Authorization", "Bearer " . $this->accessToken);
$apiClient = new DocuSign\eSign\Client\ApiClient($config);
$envelopeApi = new DocuSign\eSign\Api\EnvelopesApi($apiClient);
return $envelopeApi->createEnvelope($this->accountId, $envelopeDefinition);
}
public function get_url_document($user, $envelopeId, $returnUrl)
{
$recipientViewRequest = new DocuSign\eSign\Model\RecipientViewRequest([
'user_name' => $user->user_name,
'email' => $user->user_email,
"recipient_id" => "1",
"client_user_id" => $user->user_id,
"authentication_method" => "email",
"return_url" => $returnUrl
]);
$config = new DocuSign\eSign\Configuration();
$config->setHost($this->api);
$config->addDefaultHeader("Authorization", "Bearer " . $this->accessToken);
$apiClient = new DocuSign\eSign\Client\ApiClient($config);
$envelopeApi = new DocuSign\eSign\Api\EnvelopesApi($apiClient);
return $envelopeApi->createEnvelopeRecipientSharedView($this->accountId, $envelopeId, $recipientViewRequest);
}
I couldn't find ANYTHING related to this error on the documentation and I checked all the permissions and everything seems ok. I'm using the admin user of my demoaccount. Any ideas what I'm doing wrong here?
Thanks!
SHARED_VIEW_USER_LACKS_PERMISSION is about the user and the account. You may want to try a different account and/or a new envelope. I would also ensure that you are making API call to demo.docusign.net URL and not www.docusign.net since you are still in demo/sandbox.
The accessToken should match the account and if you're using the token generator, it's the account that you used when token generator prompted you to log in.

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