PHP mail() send files, don't understand boundary delimiter - phpmailer

trying to send email via function mail()
I receive an email without files. It was working sending only one file too.
But this doesn't work sending multiple files.
I don't understand how to use boundary? So my code doesn't work.
$passage_ligne = "\r\n";
$boundary = "-----=".md5(rand());
$boundary_alt = "-----=".md5(rand());
$headers = 'From:'.$nom.' <'.$email.'>' . "\r\n";
$headers .= 'Return-Path: '.$nom.' <'.$email.'>'.$passage_ligne;
$headers .= "Message-ID:<".time()." TheSystem#".$_SERVER['SERVER_NAME'].">".$passage_ligne;
$headers .= "X-Mailer: PHP v".phpversion().$passage_ligne;
$headers.= "MIME-Version: 1.0".$passage_ligne;
$headers.= "Content-Type: multipart/mixed;".$passage_ligne." boundary=\"$boundary\"".$passage_ligne;
$corps = $passage_ligne."--".$boundary.$passage_ligne;
$corps.= "Content-Type: multipart/alternative;".$passage_ligne." boundary=\"$boundary_alt\"".$passage_ligne;
$corps.= $passage_ligne."--".$boundary_alt.$passage_ligne;
$corps.= "Content-Type: text/plain; charset=\"ISO-8859-1\"".$passage_ligne;
$corps.= "Content-Transfer-Encoding: 8bit".$passage_ligne;
$corps.= $passage_ligne.$message.$passage_ligne;
$corps.= $passage_ligne."--".$boundary_alt.$passage_ligne;
if(isset($_FILES["file"]) && $_FILES['file']['name'] != "")
{
$nom_fichier = $_FILES['file']['name'];
$source = $_FILES['file']['tmp_name'];
$type_fichier = $_FILES['file']['type'];
$taille_fichier = $_FILES['file']['size'];
foreach($_FILES as $photo){
$tmp_name = $photo['tmp_name'];
$type = $photo['type'];
$name = $photo['name'];
$size = $photo['size'];
if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'r');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$corps .= $passage_ligne."--".$boundary."--".$passage_ligne;
$corps .= "Content-Type: ".$type."; name=\"".$name."\"".$passage_ligne;
$corps .= "Content-Transfer-Encoding: base64".$passage_ligne;
$corps .= "Content-Disposition: attachment; filename='".$name."'".$passage_ligne;
$corps .= $passage_ligne.$data.$passage_ligne.$passage_ligne;
}
}
$corps.= $passage_ligne."--".$boundary."--".$passage_ligne;
Someone could explain this boundary to me please? How to use it?
Thanks.

Related

How to properly send documents for signing using docusign API in php

Am trying to send Documents for signing to a recipient using Docusign. The application was able to print account id and baseurl in step 1
My Issue is that in step 2, it throws error
error calling webservice, status is:401 error text is --> { "errorCode": "USER_AUTHENTICATION_FAILED", "message": "One or both of Username and Password are invalid." }
Here is the code
<?php
// Input your info here:
$integratorKey = 'Integrator key goes here';
$email = 'login email goes here';
$password = 'password goes here';
$recipient_email = 'recipient#gmail.com';
$name = 'Recipient Name';
$document_name = 'World_Wide_Corp_lorem.pdf';
// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (to retrieve baseUrl and accountId)
/////////////////////////////////////////////////////////////////////////////////////////////////
$url = "https://demo.docusign.net/restapi/v2/login_information";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
echo "error calling webservice, status is:" . $status;
exit(-1);
}
$response = json_decode($json_response, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);
//--- display results
echo "\naccountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Create an envelope with one recipient, one tab, one document and send!
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = "{
\"emailBlurb\":\"This comes from PHP\",
\"emailSubject\":\"DocuSign API - Please Sign This Document...\",
\"documents\":[
{
\"documentId\":\"1\",
\"name\":\"".$document_name."\"
}
],
\"recipients\":{
\"signers\":[
{
\"email\":\"$recipient_email\",
\"name\":\"$name\",
\"recipientId\":\"10\",
\"tabs\":{
\"signHereTabs\":[
{
\"anchorString\":\"Signature:\",
\"anchorXOffset\":\"0\",
\"anchorYOffset\":\"0\",
\"documentId\":\"1\",
\"pageNumber\":\"1\"
}
]
}
}
]
},
\"status\":\"sent\"
}";
$file_contents = file_get_contents($document_name);
$requestBody = "\r\n"
."\r\n"
."--myboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$data\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"World_Wide_Corp_lorem.pdf\"; documentid=1 \r\n"
."\r\n"
."$file_contents\r\n"
."--myboundary--\r\n"
."\r\n";
// *** append "/envelopes" to baseUrl and as signature request endpoint
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data;boundary=myboundary',
'Content-Length: ' . strlen($requestBody),
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
exit(-1);
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
//--- display results
echo "Document is sent! Envelope ID = " . $envelopeId . "\n\n";
Old Legacy Auth is no longer supported. Not sure where you found this code or any documentation showing this old way of making API calls.
You must use OAuth. You have to get an oauth token by using either Auth Code Grant or JWT and then put it in the header of your call using a "Bearer"
See code:
# You will need to obtain an access token using your chosen authentication flow
$api_client = new \DocuSign\eSign\client\ApiClient($base_path);
$config = new \DocuSign\eSign\Model\Configuration($api_client);
$config->addDefaultHeader('Authorization', 'Bearer ' + $access_token);
$users_api = new \DocuSign\eSign\Api\UsersApi($api_client);

PHP undelivered mail

trying sending some mail using function mail()
$passage_ligne = "\r\n";
$boundary = "-----=".md5(rand());
$headers = 'From:'.$nom.' <'.$email.'>' . "\r\n";
$headers .= 'Return-Path: '.$nom.' <'.$email.'>'.$passage_ligne;
$headers .= "Message-ID:<".time()." TheSystem#".$_SERVER['SERVER_NAME'].">".$passage_ligne;
$headers .= "X-Mailer: PHP v".phpversion().$passage_ligne;
$headers.= "MIME-Version: 1.0".$passage_ligne;
$headers.= "Content-Type: multipart/related; boundary=\"$boundary\"".$passage_ligne;
if(file_exists($file))
{
$file_type = filetype($file);
$file_size = filesize($file);
$handle = fopen($file, 'r') or die('File '.$file.'can t be open');
$content = fread($handle, $file_size);
$content = chunk_split(base64_encode($content));
$f = fclose($handle);
$corps = '--'.$boundary."\r\n";
$corps .= 'Content-type:'.$file_type.';name='.$file."\r\n";
$corps .= 'Content-transfer-encoding:base64'."\r\n";
$corps .= "Content-Disposition: attachment; filename='".$file."'\r\n";
$corps .= $content."\r\n";
}
In the undelivered message, it says:
Host or domain name not found. Name service error for name=xxx-xxx.com type=A: Host not found
Can't see whats wrong please?

php mail() function taking too much time to execute on centos6 server

I have written a small code to check the speed of my server. BUt it takes almost 60 seconds to finish execution.
<?php
$start=time();
$to = "test#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:test#mydomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
$after=time();
$total=$after-$start;
echo "</br>";
echo "Total execution time : ".$total." seconds";
?>
What maybe the reason? and how can I improve the execution time?
I already increased the memory limit in php.ini file.
It was an issue with sendmail mailserver. Installing postfix mailserver solved the issue.

Adding document(s) to draft envelope

Does anyone have any success adding documents to a draft envelope following the examples in the REST API?
Add Documents to a Draft Envelope - I can't get this to work at all.
Add a Document to a Draft Envelope - I have partial success. On the web console, I can see the document is added to the envelope, but when trying to open the document, the document shows "Unable to load PDF". The link to the document looks like https://demo.docusign.net/Member/noname.pdf?d=00000000-0000-0000-0000-000000000000&...
The PUT request that I used for #2:
X-DocuSign-Authentication: {"SendOnBehalfOf": "xxx", "Username":"xxx","Password":"xxx","IntegratorKey":"xxx"}
Accept: application/json
Content-Type: application/pdf
Content-Disposition: file; filename="api_upload.pdf"; documentId=3; fileExtension="pdf"
Content-Transfer-Encoding: base64
[base64 encoded bytes removed]
Any suggestions will be appreciated.
I just tested and was able to add two documents to a draft envelope with the following PHP code. If you enter your credentials this code will Login, Create a Draft Envelope, then add 2 documents to that draft envelope, all in one go.
To run this code just:
Save file locally.
Enter credentials at top.
Copy two pdfs to same directory, name them document1.pdf and document2.pdf.
Run the code.
Note: The REST API documentation for this call has some inaccuracies, the below request body works so follow this instead...
<?php
// Input your info here:
$integratorKey = '...';
$email = '...';
$password = '...';
$name = 'John Doe';
// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (to retrieve baseUrl and accountId)
/////////////////////////////////////////////////////////////////////////////////////////////////
$url = "https://demo.docusign.net/restapi/v2/login_information";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
echo "error calling webservice, status is:" . $status;
exit(-1);
}
$response = json_decode($json_response, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);
//--- display results
echo "\naccountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Create a draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
"emailBlurb" => "This comes from PHP",
"emailSubject" => "DocuSign API Testing",
"status" => "created");
$data_string = json_encode($data);
// *** append "/envelopes" to baseUrl and as signature request endpoint
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
return;
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
//--- display results
echo "Document is created! Envelope ID = " . $envelopeId . "\n\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 3 - Add documents to draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
"documents" => array(
array( "documentId" => "1", "name" => "document1.pdf", "order" => "1" ),
array( "documentId" => "2", "name" => "document2.pdf", "order" => "2" )
));
$data_string = json_encode($data);
$file_contents1 = file_get_contents("document1.pdf");
$file_contents2 = file_get_contents("document2.pdf");
$requestBody = "\r\n"
."\r\n"
."--myboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$data_string\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document1.pdf\"; documentid=1 \r\n"
."\r\n"
."$file_contents1\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document2.pdf\"; documentid=2 \r\n"
."\r\n"
."$file_contents2\r\n"
."--myboundary--\r\n"
."\r\n";
// *** append "/envelopes/{envelopeId}/documents" to baseUrl and as endpoint
$url = $baseUrl . "/envelopes/$envelopeId/documents";
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data;boundary=myboundary',
'Content-Length: ' . strlen($requestBody),
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($response); echo "\n";
exit(-1);
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
//--- display results
echo "Document is sent! Envelope ID = " . $envelopeId . "\n\n";
?>
One last thing I'll note, is make sure you are doing a PUT request for this call and not a POST, as that's a common mistake I see here.

DocuSignAPI embedded signing session redirect url

Im fairly new to implementing the embedded signing using the rest api. I am able to start the signing session within the iframe but after finishing the document/template it is redirecting me to a url.
Can anybody please let me know how or where do i configure the redirect url, i guess the url is appended with the status from where i can use this token within my app.
NeverMind.. i got it, can't believe i dint see that in the first place.
// STEP 3 - Get the Send View
String reqBody = "<recipientViewRequest xmlns=\"http://www.docusign.com/restapi\">" +
"<authenticationMethod>email</authenticationMethod>" +
"<email>test#email.com</email>" +
**"<returnUrl>http://www.docusign.com</returnUrl>" +**
"<clientUserId>1</clientUserId>" +
"<userName>" + recipientName + "</userName>" +
"</recipientViewRequest>";
Looks like you answered your own question but for the benefit of the community, one of the DocuSign API recipes demonstrates all the steps for Embedded Signing.
Here is the full PHP version of the code. As you've found out, you can set the URL where users get redirected to after signing by setting the returnUrl property in your request body.
Here's the full PHP program for Embedded Signing. You can also find this here
<?php
// Input your info:
$integratorKey = '...';
$email = '...#....com';
$password = '...';
$name = "John Doe";
// copy the templateId of an existing template here
$templateId = "C9D9D181-CE57-.....................";
// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (retrieves baseUrl and accountId)
/////////////////////////////////////////////////////////////////////////////////////////////////
$url = "https://demo.docusign.net/restapi/v2/login_information";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
echo "error calling webservice, status is:" . $status;
exit(-1);
}
$response = json_decode($json_response, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);
//--- display results
echo "accountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Create an envelope with an Embedded recipient (uses the clientUserId property)
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array("accountId" => $accountId,
"emailSubject" => "Hello World!",
"emailBlurb" => "This comes from PHP",
"templateId" => $templateId,
"templateRoles" => array(
array( "email" => $email, "name" => $name, "roleName" => "Signer1", "clientUserId" => "1001" )),
"status" => "sent");
$data_string = json_encode($data);
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
exit(-1);
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
curl_close($curl);
//--- display results
echo "Envelope created! Envelope ID: " . $envelopeId . "\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 3 - Get the Embedded Singing View
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array("returnUrl" => "http://www.docusign.com/devcenter",
"authenticationMethod" => "None", "email" => $email,
"userName" => $name, clientUserId => "1001"
);
$data_string = json_encode($data);
$curl = curl_init($baseUrl . "/envelopes/$envelopeId/views/recipient" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
exit(-1);
}
$response = json_decode($json_response, true);
$url = $response["url"];
//--- display results
echo "Embedded URL is: \n\n" . $url . "\n\nNavigate to this URL to start the embedded signing view of the envelope\n";
?>

Resources