从不确定的电子邮件组中随机选择三封电子邮件
Choose three emails at random from an indefinite group of emails
第一次写到这里,如有错误请见谅。
我不是程序员,我通常会尝试研究您在此站点上提供的建议,并找到一种方法将其应用到我的需求中,但这次我就是找不到解决方案。
我使用 wordpress 的电子邮件插件,它允许我在“Bcc”字段中放置一个像这样的 [emails-group] 标签,这个标签可以包含一个或多个用逗号分隔的电子邮件地址。
使用我在下面写的代码(我不知道它是否以最好的方式编写)我可以在电子邮件地址为三个时向两个随机电子邮件地址发送电子邮件。 (此代码在我的测试中有效)
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
);
我需要的是一个新代码:
如果电子邮件地址是一个或两个或三个,则向所有人发送电子邮件,但如果电子邮件地址超过三个,则只随机选择三个电子邮件地址并向他们发送电子邮件。
感谢任何能帮助我找到解决方案的人。
问候,
拉斐尔.
变量 $expl
似乎有所有电子邮件地址的列表。
尝试:
$list = $expl;
$rand_keys = array_rand($list, 3);
$first = $list[$rand_keys[0]];
$second = $list[$rand_keys[1]];
$third = $list[$rand_keys[1]];
我的最终代码是:
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
);
非常感谢 Luuk 的到来。
你让我的星期天变得更好
第一次写到这里,如有错误请见谅。 我不是程序员,我通常会尝试研究您在此站点上提供的建议,并找到一种方法将其应用到我的需求中,但这次我就是找不到解决方案。 我使用 wordpress 的电子邮件插件,它允许我在“Bcc”字段中放置一个像这样的 [emails-group] 标签,这个标签可以包含一个或多个用逗号分隔的电子邮件地址。 使用我在下面写的代码(我不知道它是否以最好的方式编写)我可以在电子邮件地址为三个时向两个随机电子邮件地址发送电子邮件。 (此代码在我的测试中有效)
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
);
我需要的是一个新代码:
如果电子邮件地址是一个或两个或三个,则向所有人发送电子邮件,但如果电子邮件地址超过三个,则只随机选择三个电子邮件地址并向他们发送电子邮件。
感谢任何能帮助我找到解决方案的人。 问候, 拉斐尔.
变量 $expl
似乎有所有电子邮件地址的列表。
尝试:
$list = $expl;
$rand_keys = array_rand($list, 3);
$first = $list[$rand_keys[0]];
$second = $list[$rand_keys[1]];
$third = $list[$rand_keys[1]];
我的最终代码是:
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
);
非常感谢 Luuk 的到来。 你让我的星期天变得更好