it's the first time I'm writing here, sorry for any mistakes. I'm not a programmer, I usually try to study the advice you give on this site and find a way to apply it to my needs but this time I just can't find a solution. I use an email plugin for wordpress that allows me to put a tag like this [emails-group] in the "Bcc" field, this tag can contain one or more email addresses separated by commas. with the code I wrote below (I don't know if it's written in the best way) I can send an email to two random email addresses when the email addresses are three. (this code works in my tests)
add_filter( 'wpcf7_mail_tag_replaced',
function( $replaced, $submitted, $html, $mail_tag ) {
if ( 'emails-group' == $mail_tag->field_name() ) {
foreach($submitted as $value){
$expl = explode(",", $value);
$list = array("$expl[0]","$expl[1]","$expl[2]");
$rand_keys = array_rand($list, 2);
$first = $list[$rand_keys[0]];
$second = $list[$rand_keys[1]];
$result = array("$first","$second");
$replaced = implode(",",$result);
}
}
return $replaced;
},
10, 4
);
what I would need is a new code for:
if the email addresses are one or two or three then send everyone the email, but if the email addresses are more than three choose only three email addresses at random and send them the email.
I thank anyone who can put me on the right way to find the solution. Greetings, Raffaele.
CodePudding user response:
The variable $expl
seems to have a list of all the email addresses.
Try:
$list = $expl;
$rand_keys = array_rand($list, 3);
$first = $list[$rand_keys[0]];
$second = $list[$rand_keys[1]];
$third = $list[$rand_keys[1]];
CodePudding user response:
my final code that works for me is:
add_filter( 'wpcf7_mail_tag_replaced',
function( $replaced, $submitted, $html, $mail_tag ) {
if ( 'emails-group' == $mail_tag->field_name() ) {
foreach($submitted as $value){
$expl = explode(",", $value);
if (count($expl) >= 3){
$list = $expl;
$rand_keys = array_rand($list, 3);
$first = $list[$rand_keys[0]];
$second = $list[$rand_keys[1]];
$third = $list[$rand_keys[2]];
$result = array("$first","$second","$third");
$replaced = implode(",",$result);
} else {
$replaced = $value;
}
}
}
return $replaced;
},
10, 4
);
thank you very much to Luuk for his availability. You made my Sunday better