這是我第一次寫在這里,如有錯誤,請見諒。我不是程式員,我通常會嘗試研究您在此站點上提供的建議并找到一種方法將其應用于我的需求,但這次我找不到解決方案。我為 wordpress 使用了一個電子郵件插件,它允許我在“密件抄送”欄位中放置一個像 [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
);
我需要的是一個新代碼:
如果電子郵件地址是一兩個或三個,則向所有人發送電子郵件,但如果電子郵件地址超過三個,則僅隨機選擇三個電子郵件地址并向他們發送電子郵件。
我感謝任何能讓我找到解決方案的人。你好,拉斐爾。
uj5u.com熱心網友回復:
該變數$expl似乎包含所有電子郵件地址的串列。
嘗試:
$list = $expl;
$rand_keys = array_rand($list, 3);
$first = $list[$rand_keys[0]];
$second = $list[$rand_keys[1]];
$third = $list[$rand_keys[1]];
uj5u.com熱心網友回復:
對我有用的最終代碼是:
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 的出席。你讓我的星期天變得更好
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/388529.html
