I need to send emails to multiple recipients. The number of recipients will vary depending on the data in the db.
Mandrill allows me to only add multiple recipients using an array.
Below is what works for multiple recipients
//email array that needs to be added to the 'to' key
$emailArray = ["example#example.com","test#test.com","hello#test.com","world#test.com"];
$mandrill = new Mandrill('xxxxxxxxxxxxxxx');
$message = array(
'subject' => 'Thanks for signing up',
'from_email' => 'support#test.com',
'to' => array(
array(
'email' => 'hello#test.com',
'name' => 'Hello Test'
),
array(
'email' => 'goodbye#test.com',
'name' => 'Goodbye Test',
)
),
'global_merge_vars' => array(
array(
'name' => 'FIRSTNAME',
'content' => 'JOHN'
),
array(
'name' => 'LASTNAME',
'content' => 'DOE')
));
//print_r($message);
$template_name = 'hello-world';
print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
Below is what i need to generate dynamically depending on the length of the emailArray
to' => array(
//the below array should be dynamically generated
array(
'email' => 'hello#test.com',
'name' => 'Hello Test'
),
array(
'email' => 'goodbye#test.com',
'name' => 'Goodbye Test',
)
)
Here is the array. Currently with fixed values.
//email array that needs to be added to the 'to' key
$emailArray = ["example#example.com","test#test.com","hello#test.com","world#test.com"];
My questions is How do generate the 'To' values based on the length of the email array?
is there a way i can implode the entire array script?
An effective way would be to use array_map I have added in some code that also takes an array of names as well (I couldn't see where you were getting the names from in your code).
$toAddresses = array('example#example.com','test#test.com','hello#test.com','world#test.com');
$names = array('Exmaple', 'Test', 'Hello', 'World');
$mandrillTo = array_map( function ($address, $name) {
return array(
'email' => $address,
'name' => $name
);
},
$toAddresses,
$names
);
This passes the 0th element from each array into the function and returns an array of two values, then passes the 1st, 2nd etc and returns each result in a new array ($mandrillTo)
Related
This question already has answers here:
PHP is there a way to add elements calling a function from inside of array
(3 answers)
Closed last month.
In the middle of declaring an array of arrays, I want to "write" an array of arrays generated by my function.
I have a working example when I:
simply store my function-generated arrays into a variable and
then call each array from that function by its key,
but I can't find a command to simply call everything at once.
Here is the code which (I hope) explains it:
<?php
// A. (THIS WORKS)
// A1: A function that returns an array of arrays
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// ... and so on, many more.
return array(
'first-array' => $first_array,
'second-array' => $second_array,
// ... and so on.
);
// NOTE there are tens or hundreds of returned arrays here.
}
// A2: Store my arrays in a variable
$my_array = my_arrays_building_function();
// A3: Inside an array (of arrays), I simply "write" my arrays INDIVIDUALLY and THAT works
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
// HERE THERY ARE, INDIVIDUALLY, COMMA SEPARATED
$my_array[ 'first-array' ],
$my_array[ 'second-array' ],
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
),
/** -------------------- //
THE ISSUE
// -------------------- **/
// B: HOW DO I "write" THEM ALL AT ONCE???
// B1: The same as A1
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// NOT SURE I SHOULD RETURN LIKE THIS
return array(
'first-array' => $first_array,
'second-array' => $second_array
);
}
// B2: Same as A3, Inside an array (of arrays), I "write" my arrays BUT NOW I WANT TO "WRITE" THEM ALL AT ONCE
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
/** >>>> I need my arrays here ALL AT ONCE aka NOT INDIVIDUALLY AS IN EXAMPLE A. <<<< **/
/**
* In other words, while I'm declaring this array,
* I simply need all my arrays from my_arrays_building_function()
* "written" here with a simple command instead of calling hundreds
* of arrays individually as in the first example
*/
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
), /* this goes on as it's a part of even bigger array */
Although I wouldn't recommend declaring hundreds of array variables inside a function because that's crazy, but for now, you can use get_defined_vars() to get over this issue.
You will also need to filter out the variables which are arrays and has the keys id, type and title as there are could be several other variables defined apart from this.
Snippet:
<?php
array_filter(get_defined_vars(), fn($val) => is_array($val) && isset($val['id'], $val['type'], $val['title']));
Online Demo
Not too sure if I'm understanding this correctly but from what I assume, you just want to return an array with a bunch of others inside it that you define throughout the function?
A simple approach for this would be to define your output variable immediately and add all of your other arrays to it:
function my_arrays_building_function() {
$output = [];
$output[] = [
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
];
$output[] = [
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
];
return $output;
}
Thank you to #Rylee for suggesting Array Unpacking.
The final code would look like this:
// C1: A function that returns an array of arrays BUT without keys
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// ... and so on, many more.
// Then return but now I don't assign keys, I only list the vars.
return array(
$first_array, $second_array, ... and so on.
);
}
// C2: Inside an array (of arrays), I use Array Unpacking, THAT'S WHAT I WAS LOOKING FOR, UNPACK ARRAY! SEE BELOW
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
// HERE I UNPACK MY ARRAY BY USING ... THE THREE DOTS ARE THE KEY
... my_arrays_building_function(),
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
),
Hooray!
Whenever the "to" value is hardcoded, the email is sent. However, when the string value is replaced with a string variable the email does not get sent.
$result = $mgClient->sendMessage($domain, array(
'from' => 'Eemayl Ok <mailgun#domain.net>',
'to' => $customerEmail,
'to' => Tools::safeOutput($customerEmail),
'to' => (string)$customerEmail,
'to' => $customer->email,
'to' => Tools::safeOutput($customer->email),
'to' => 'hardcoded_email#gmail.com',
'subject' => 'We Hope You get this Email!',
'text' => '',
'html' => '<html>Contact Us Ok??</a></html>'
));
The several "to"s are my attempt at variations of expressing the variable value.
Array does not accept duplicate keys. It only chooses the last value if key duplicated. According to mailgun API, You should use commas to separate multiple recipients.
$recipients = array('client1#gmail.com', 'client2#gmail.com');
$result = $mgClient->sendMessage($domain, array(
'from' => 'Eemayl Ok <mailgun#domain.net>',
'to' => implode(',', $recipients),
'subject' => 'We Hope You get this Email!',
'text' => '',
'html' => '<html>Contact Us Ok??</a></html>'
));
I am sending email using merge_vars for dynamic contents. Here is how my merge_vars look like:
$message['merge_vars'][$index] = array(
'rcpt' => $email,
'vars' => array(
array(
'name' => 'url',
'content' => $url
),
array(
'name' => 'sname',
'content' => $sname
),
array(
'name' => 'lname',
'content' => $lname,
),
array(
'name' => 'email',
'content' => $email
)
),
);
Everything is working fine. But when same recipient should receive multiple different email in a single API call, then the problem occurs. That time same recipient don't receive different email, he receives same email multiple times.
In a single api call you cannot send different emails to same email address with different merge vars.
You need to fire multiple api calls having target recipient in each call with respective merge_vars to meet your requirement.
I have the following two things:
$_POST array with posted data
$params array with a path for each param in the desired data array.
$_POST = array(
'name' => 'Marcus',
'published' => 'Today',
'url' => 'http:://example.com',
'layout' => 'Some info...',
);
$params = array(
'name' => 'Invoice.name',
'published' => 'Page.published',
'url' => 'Page.Data.url',
'layout' => 'Page.Data.layout',
);
I would like to generate the $data array like the example below.
How can I do that?
Notice how the "paths" from the $params array are used to build the keys for the data array, filling it with the data from the $_POST array.
$data = array(
'User' => array(
'name' => 'Marcus',
),
'Page' => array(
'published' => 'Today',
'Data' => array(
'url' => 'http:://example.com',
'layout' => 'Some info...',
),
),
);
I would use referenced variables:
$post = array( // I renamed it
'name' => 'Marcus',
'published' => 'Today',
'url' => 'http:://example.com',
'layout' => 'Some info...',
);
$params = array(
'name' => 'Invoice.name',
'published' => 'Page.published',
'url' => 'Page.Data.url',
'layout' => 'Page.Data.layout',
);
echo '<pre>'; // just for var_dump()
foreach($post as $key=>$var){ // take each $_POST variable
$param=$params[$key]; // take the scheme fields
$path=explode('.',$param); // take scheme fields as an array
$temp=array(); // temporary array to manipulate
$temp_original=&$temp; // we need this the same as we're going to "forget" temp
foreach($path as $pathvar){ // take each scheme fields
$temp=&$temp[$pathvar]; // go deeper
}
$temp=$var; // that was the last one, insert it
var_dump($temp_original); // analize carefully the output
}
All you have to do now is to combine them all, they are not exactly what you want, but this will be easy.
And please note, that each $temp_original fields are pointing at $post variable data! (&string instead of string). You may want to clone it somehow.
we are using SAOP clients for a while now without a problem. But now we are facing the following challenge and I can't find the answer. We need to send the following XML structure:
<Cards>
<CardDetails>
<Name>string</Name>
<Address>string</String>
</CardDetails>
<CardDetails>
<Name>string</Name>
<Address>string</String>
</CardDetails>
</Cards>
As you van see we need two instances of 'CardDetails'. Creating a PHP array will only allow me to send 1.
$data = array(
'Cards' => array(
'CardDetails' => array(
'Name' => 'test name',
'Address' => 'test address'
),
'CardDetails' => array(
'Name' => 'second test name',
'Address' => 'second test address'
)
)
));
Of course, only the second address will be used. But what would be the solution to make this work?
Thanks a lot!
Use php dom to create xml from an array , here is a good example of it http://www.ibm.com/developerworks/library/os-xmldomphp/
And what function/class are you using to turn array to xml?
Try this structure:
$data = array(
'Cards' => array(
'CardDetails' => array(
array(
'Name' => 'test name',
'Address' => 'test address'
),
array(
'Name' => 'second test name',
'Address' => 'second test address'
)
)
)
);
If this doesn't work and you can modify serializing function, just check if current key is numerical. If it is, use parent key name for tag.