Can't set variable in string - php

So I'm trying to send a dynamic email with PHP. Now here's what I have
$postString = '{
"key": "xxx",
"message": {
"html": "this is the emails html content",
"text": "this is the emails text content",
"subject": "this is the subject",
"from_email": "email#email.com",
"from_name": "Joe",
"to": [
{
"email": "Joe# Joe",
"name": "Joe# Joe"
}
],
"attachments": [
]
},
"async": false
}';
Now I want "html" to be a variable. So I did this
"html": $var,
Sadly that doesn't work. Not does {} or using single quotes. Any ideas? It gets picked up as a string, by the way.

Variables are not interpolated in strings delimited by single quotes. There are a few ways around this. The following example uses concatenation.
$postString = '{
"key": "xxx",
"html": "' . $var . '",
"message": {
"html": "this is the emails html content",
"text": "this is the emails text content",
"subject": "this is the subject",
"from_email": "email#email.com",
"from_name": "Joe",
"to": [
{
"email": "Joe# Joe",
"name": "Joe# Joe"
}
],
"attachments": [
]
},
"async": false
}';
To be honest with you, this would be a lot easier if you just used an array and then encoded it into JSON using json_encode().

As mentioned in my comment, this would work a lot better
$post = [
'key' => 'xxx',
'message' => [
'html' => $var,
'text' => 'this is the emails text content',
'subject' => 'this is the subject',
'from_email' => 'email#email.com',
'to' => [
['email' => 'Joe# Joe', 'name' => 'Joe# Joe']
],
'attachments' => []
],
'async' => false
];
$postString = json_encode($post);
Obligatory legacy PHP note: If you're stuck on a PHP version lower than 5.4, you obviously can't use the shorthand array notation. Substitute [] with array() if that's the case.

Related

Need help. Prepared envelope registered but not sent to email

I am just starting to work this lib and need some help in first steps.
I need to have template for sending to defined in code recipients with prefilled data.
I have template with fullName field and one sign field
In code I am creating new envelope. Code example is:
$envelopeDefinition = new \DocuSign\eSign\Model\EnvelopeDefinition(
[
'status' => 'sent',
'template_id' => $templateId,
]
);
$signer = new \DocuSign\eSign\Model\TemplateRole(
[
'email' => $email,
'name' => $name,
'role_name' => 'signer',
'client_user_id' => 1000,
'tabs' => new \DocuSign\eSign\Model\Tabs(
[
'sign_here_tabs' => [
new \DocuSign\eSign\Model\SignHere(['tab_label' => 'sign'])
]
]
)
]
);
$envelopeDefinition->setTemplateRoles([$signer]);
$envelopeApi = new \DocuSign\eSign\Api\EnvelopesApi($authService->getAuthorizedApiClient());
$envelopeApi->createEnvelope($accountId, $envelopeDefinition);
Envelope was created and it is displayed in manage tab in DocusignUI
In history there are only register event listed. I can't see any sending invitations to email event
No email was received
Could you explain what's wrong with my code?
Addition
I am also trying to create new template in similar way as in EG008CreateTemplate from examples in short version to check if there are any problems with my template configuration.
My request sends post-data:
{
"description": "Example template created via the API",
"documents": [
{
"documentBase64": "...",
"documentId": "1",
"fileExtension": "pdf",
"name": "Debug Agreement"
}
],
"emailSubject": "Please sign this document",
"name": "Debug Agreement Template",
"recipients": {
"signers": [
{
"recipientId": "1",
"roleName": "signer",
"routingOrder": "1",
"tabs": {
"numberTabs": [
{
"documentId": "1",
"font": "helvetica",
"fontSize": "size14",
"pageNumber": "1",
"required": "false",
"tabLabel": "numbersOnly",
"width": "84",
"xPosition": "163",
"yPosition": "260"
}
],
"signHereTabs": [
{
"documentId": "1",
"pageNumber": "1",
"xPosition": "191",
"yPosition": "148"
}
],
"textTabs": [
{
"documentId": "1",
"font": "helvetica",
"fontSize": "size14",
"height": "23",
"pageNumber": "1",
"required": "false",
"tabLabel": "text",
"width": "84",
"xPosition": "153",
"yPosition": "230"
}
]
}
}
]
},
"shared": "false",
"status": "created"
}
And receives again error The request body is missing or improperly formatted. New Account definition(s) not found in the request body.
Who can explain what does this error means? I can't find any descriptions for this case
Take out this line:
'client_user_id' => 1000
This line is used for embedded signing, which is not sending an email, because you embed it in your app.
You want remote signing with an email, so you shouldn't provide a client_user_id.
After digging up code and request process it was detected that accountId was empty and request called https://demo.docusign.net/restapi/v2.1/accounts//templates
Provided error message is not correct as I tend to think.
It is strange for me that I was able to get templates list, create new envelopes with empty accountId...

Laravel Validation is not throwing the error of the field that is really faulty

I have a validation block as follows:
$this->validate($request, [
'id' => 'required|string|unique:user,id|max:32',
'email' => 'required|email|max:191',
'name' => 'nullable|string',
'birthDate' => 'nullable|date_format:Y-m-d',
'countryId' => 'nullable|integer',
'city' => 'nullable|string|max:191',
'address' => 'nullable|string',
'zipCode' => 'nullable|string|max:191',
'phone' => 'nullable|string',
]);
I'm sending data like this:
{
"id": "nJy8zWQ6VuptDFNA",
"email": "email#email.com",
"name": "name",
"birthDate": "1980-01-01",
"countryId": 1481,
"city": "a city",
"address": "this is an address",
"zipCode": "123400",
"phone": 09876554321
}
I am sending the phone field as improper data-type. Then the response is like that the phone field is wrong type.
But I get this response:
{
"id": [
"The id field is required."
],
"email": [
"The email field is required."
]
}
I can't find the problem here.
The problem is that you are not sending a valid JSON body.
This code:
$json = <<<JSON
{
"id": "nJy8zWQ6VuptDFNA",
"email": "email#email.com",
"name": "name",
"birthDate": "1980-01-01",
"countryId": 1481,
"city": "a city",
"address": "this is an address",
"zipCode": "123400",
"phone": 09876554321
}
JSON;
json_decode($json);
echo json_last_error();
Will echo 4 which is the code for JSON_ERROR_SYNTAX meaning that is a syntax error.
The error is that numbers can't be prefixed with 0 in JSON. It's probably because in JavaScript prefixing with 0 indicates an octal number but allowing this in JSON could hurt portability.
Unfortunately, the default behaviour of the PHP built-in JSON parser is to return null on a syntax error and not say anything else about it.
This could potentially be a Laravel idea to allow validating that the entire input is correctly formatted JSON as part of the validation to ensure that we have means of checking that what we send is correct.
The phone number is missing the quotes and its making the json invalid.
You're supposed to add the "(quotes) for all the values, you've missed it for phone
it supposed to be like this
{
"id": "nJy8zWQ6VuptDFNA",
"email": "email#email.com",
"name": "name",
"birthDate": "1980-01-01",
"countryId": 1481,
"city": "a city",
"address": "this is an address",
"zipCode": "123400",
"phone": "09876554321"
}

Insert a Variable inside simple quotation marks PHP

How i can use a variable ($_REQUEST('subject')) inside simple quotation marks.
This is my code:
<?php
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$postString = '{//i can't quit this quotation mark
"key": "myapi",
"message": {
"html": "this is the emails html content",
"subject": "$_REQUEST['subject'];",//this dont work
"from_email": "email#mydomain.com",
"from_name": "John",
"to": [
{
"email": "test#hotmail.com",
"name": "Bob"
}
],
"headers": {
},
"auto_text": true
},
"async": false
}';
?>
That's JSON! Use json_encode and json_decode!
$json = json_decode ($postString, true); // true for the second parameter forces associative arrays
$json['message']['subject'] = json_encode ($_REQUEST);
$postString = json_encode ($json);
Although, it looks like you could save a step and yourself some trouble if you just build $postString as a regular php array.
$postArr = array (
"key" => "myapi",
"message" => array (
"html" => "this is the emails html content",
"subject" => $_REQUEST['subject'],
"from_email" => "email#mydomain.com",
"from_name" => "John",
"to" => array (
array (
"email" => "test#hotmail.com",
"name" => "Bob"
)
),
"headers" => array (),
"auto_text" => true
),
"async" => false
);
$postString = json_encode ($postArr);
change "subject": "$_REQUEST['subject'];" to "subject": "' . $_REQUEST['subject'] . '"
Try this:
$postString = '{
"key": "myapi",
"message": {
"html": "this is the emails html content",
"subject": "'.$_REQUEST['subject'].'", // Modify this way
"from_email": "email#mydomain.com",
"from_name": "John",
....
.....
}';

How do I replace static text with dynamic variable in Mandrill script

I want to send emails to users once they fill and submit a form, I used POST method to capture the data, now I would like to replace static text with captured variables from the form. I have provide some variable samples of data from the form.
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$postString = '{
"key": "xxxxxxxxxxxxx",
"message": {
"html": "this is the emails html content",
"text": "this is the emails text content",
"subject": "this is the subject",
"from_email": "me#gmail.com",
"from_name": "John",
"to": [
{
"email": "me#yahoo.com",
"name": "Bob"
}
],
"headers": {
},
"track_opens": true,
"track_clicks": true,
"auto_text": true,
"url_strip_qs": true,
"preserve_recipients": true,
"merge": true,
"global_merge_vars": [
],
"merge_vars": [
],
"tags": [
],
"google_analytics_domains": [
],
"google_analytics_campaign": "...",
"metadata": [
],
"recipient_metadata": [
],
"attachments": [
]
},
"async": false
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
echo $result;
Maybe something like this :
$postString = '{
"key": "xxxxxxxxxxxxx",
"message": {
"html": "'.$yourHTML.'",
"text": "'.$yourTEXT.'",
"subject": "this is the subject",
"from_email": "me#gmail.com",
"from_name": "John",
"to": [
{
"email": "me#yahoo.com",
"name": "Bob"
}
],
...
But you don't specify where you want to put the dynamic datas.
But for your case it would be better to create a PHP array and then transform it using json_encode.
Just simply replace the static data with variables:
$postString = '{
"key": "xxxxxxxxxxxxx",
"message": {
"html": "",
"text": "$message",
"subject": "this is the subject",
"from_email": "me#gmail.com",
"from_name": "John",
"to": [
{
"email": "$email",
"name": "$name"
}
],
Provide Merge Data through the API
...
"message": {
"global_merge_vars": [
{
"name": "var1",
"content": "Global Value 1"
}
],
"merge_vars": [
{
"rcpt": "emailadress#domain.com",
"vars": [
{
"name": "fname",
"content": "John"
},
{
"name": "lname",
"content": "Smith"
}
]
}
],
...
Format
Merge tags use the following format, with a pipe (|) and asterisk (*) on each side of the merge tag name:
|MERGETAG|
In your template or content, merge tags might look like this:
Dear *|FNAME|*,
Thanks for your purchase on *|ORDERDATE|* from ABC Widget Company. We appreciate your business and have included a copy of your invoice below.
*|INVOICEDETAILS|*
-- ABC Widget Co.
There are three merge tags included in the previous examples. At the time of sending, provide global values and/or recipient-specific data for each merge tag.
With Reference To:
https://mandrill.zendesk.com/hc/en-us/articles/205582487-How-to-Use-Merge-Tags-to-Add-Dynamic-Content

Creating a PHP object with out the use of a key?

I've got a request to present the data in the following format as a JSON feed:
{
"id": "123",
"info": {
"code": "ZGE",
"description": "test1",
"type": "AVL",
"date": "09/08/2012"
}
},
{
"id": "456",
"info": {
"code": "ZDN",
"description": "test2",
"type": "CLR",
"date": "16/02/2012"
}
}
However in my PHP code, I think I need to have a key itterator - but I end up with this format:
{
"0": {
"id": "123",
"info": {
"code": "ZGE",
"description": "test1",
"type": "AVL",
"date": "09/08/2012"
}
},
"1": {
"id": "456",
"info": {
"code": "ZDN",
"description": "test2",
"type": "CLR",
"date": "16/02/2012"
}
}
}
Any ideas on how to build the first data set with out having the index iterator?
simple create an array of objects, no need for the key (notice the [ ] surrounding your list)
json.txt
[{
"id": "123",
"info": {
"code": "ZGE",
"description": "test1",
"type": "AVL",
"date": "09/08/2012"
}
},
{
"id": "456",
"info": {
"code": "ZDN",
"description": "test2",
"type": "CLR",
"date": "16/02/2012"
}
}]
example.php
<?php
$data = json_decode(file_get_contents('./json.txt'));
?>
It can be built like this:
$arr = array(
array(
'id' => 123,
'info' => array(
'code' => 'ZGE',
'description' => 'test1',
'type' => 'AVL'
)
),
array(
'id' => 456,
'info' => array(
'code' => 'ZDN',
'description' => 'test2',
'type' => 'CLR'
)
)
);
echo json_encode($arr);
Outputs
[
{
"id": 123,
"info": {
"code": "ZGE",
"description": "test1",
"type": "AVL"
}
},
{
"id": 456,
"info": {
"code": "ZDN",
"description": "test2",
"type": "CLR"
}
}
]
the JSON format you've specified in the first example (ie the requested format) is not valid JSON.
A valid JSON string must evaluate to a single Javascript object; the example you've given evaluates to two Javascript objects, separated by a comma. In order to make it valid, you would need to either enclose the whole thing in square brackets, to turn it into a JS array or enclose it in curly braces, and give each of the two objects a key.
The PHP code you've written is doing the second of these two options. It is therefore generating valid JSON code, about as close to the original request as could be expected while still being valid.
It would help if you'd shown us the PHP code that you've used to do this; without that, I can't really give you advice on how to improve it, but if you want to switch to the square bracket notation, all you need is to put your PHP objects into an unkeyed array, and json_encode() should do it all for you; you shouldn't need to use a keyed array or an iterator for that.
The only reason json_encode should produce the output you're seeing is adding another named key to the array that you're passing to json_encode, by default it should work as you want:
$json = '[
{
"id": "123",
"recall_info": {
"code":"ZGE",
"description": "test1",
"type": "AVL",
"date": "09/08/2012"
}
},
{
"id": "123",
"recall_info": {
"code": "ZDN",
"description": "test2",
"type": "CLR",
"date": "16/02/2012"
}
}
]';
$php = array(
(object) array(
'id' => '123',
'recall_info' => (object) array(
'code' => 'ZGE',
'description' => 'test1',
'type' => 'AVL',
'date' => '09/08/2012'
)
),
(object) array(
'id' => '123',
'recall_info' => (object) array(
'code' => 'ZGE',
'description' => 'test2',
'type' => 'CLR',
'date' => '16/02/2012'
)
)
);
var_dump(json_encode($php));

Categories