function my_custom_popular_posts_html_list( $mostpopular, $instance ){
$output = 'https://startofwebaddress';
$weekday = date("N")-1;
$excerpt = '';
$popularPost = get_post($mostpopular[$weekday]->id);
$popularPostUrl = get_permalink( $popularPost->ID );
$featuredImgUrl = get_the_post_thumbnail_url($popularPost->ID, 'full');
$popularPostTitle = esc_html( get_the_title($popularPost) );
$popularProduct = wc_get_product( $popularPost );
$popularProductLowPrice = $popularProduct->get_price();
$excerpt = get_keywords_by_id( $mostpopular[$weekday]->id ); //most popular = 0, 2nd most = 1, 3rd most = 2, etc.
$initial = array("oldterms");
$replace = array("newterms");
$excerpt = preg_replace($initial,$replace,$excerpt);
$commonName = str_replace("+"," ",$excerpt);
$output .= $excerpt;
$output .= 'endofwebaddress';
//Get Key for second input
$First_url = $output;
$xml = new DOMDocument();
$ch = curl_init ($First_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//return xml
curl_setopt($ch, CURLOPT_HEADER, FALSE);//ignore
$xml->loadXML(curl_exec($ch));
curl_close ($ch);
$TagName1 = $xml->getElementsByTagName('TagName1')->item(0)->nodeValue;
//Email URL
$EmailURL = "urlthatneeds&TagName1=";
$EmailURL .= $TagName1;
$EmailURL .= "restofurl";
$text=file_get_contents($EmailURL);
$res = preg_match_all(
"/[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i",
$text,
$matches
);
session_start();
foreach(array_unique($matches[0]) as $email) {
$url = 'https://api.sendgrid.com/';
$user = 'user';
$pass = 'password';
$json_string = array(
'to' => $email,
'category' => 'most_popular',
'asm_group_id' => '1141',
);
}
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => 'example3@sendgrid.com', //This should be ignored
'subject' => $commonName . ' Detailed Protocols and Products.',
'html' => 'A whole ton of HTML',
'text' => 'Text Version of HTML',
'from' => 'me@mail.com',
);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);
}
add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );
function get_keywords_by_id( $post_id ){
// Get post data
$the_post = get_post( $post_id );
// Get post_content
$the_post_id = $the_post -> post_title;
return $the_post_id;
}
希望我能简化这个问题$EmailUrl包含一个包含所有电子邮件地址的字符串。我用代码中的正则表达式对其进行解析,以获得所有电子邮件地址,并将其输出到一个数组中。
根据RichGoldMD(感谢您的建议),我将session_start()移出了循环,并消除了额外的变量以直接传递电子邮件。执行此操作时,该功能只向发送电子邮件example3@sendgrid.com.同样,如果我取第一个“to”属性并将其从变量更改为逗号分隔列表,我会收到发送到逗号分隔列表的电子邮件。然而,当我尝试输入数组变量时,或者如果我尝试将数组变量按摩到逗号分隔的列表中(这是我之前所做的),那么我只会收到一封电子邮件example3@sendgrid.com.如果我将第一个“to”属性更改为[$email,'myemail@mail.com“],我收到一封电子邮件myemail@mail.com一封电子邮件发送到阵列中的最后一封电子邮件,但没有电子邮件发送到其他约1500个电子邮件地址。
谢谢