admin管理员组文章数量:1427058
How do I validate an array of emails from a textbox before sending them to the wp_mail()
function?
$emails = '[email protected];[email protected]
[email protected],[email protected], email5';
$emails = preg_split('/[;,\n]/', $emails);
$validEmails = array();
$subject = 'Hey Subject';
$message = 'I am a message';
foreach($emails as $key=>$value){
if (is_email($value)) {
array_push($validEmails, $value);
}
}
wp_mail($validEmails, $subject, $message, $headers);
The sample code above stops on the if (is_email())
condition. How can I validate each email in the array whichever way before sending to the mail function?
How do I validate an array of emails from a textbox before sending them to the wp_mail()
function?
$emails = '[email protected];[email protected]
[email protected],[email protected], email5';
$emails = preg_split('/[;,\n]/', $emails);
$validEmails = array();
$subject = 'Hey Subject';
$message = 'I am a message';
foreach($emails as $key=>$value){
if (is_email($value)) {
array_push($validEmails, $value);
}
}
wp_mail($validEmails, $subject, $message, $headers);
The sample code above stops on the if (is_email())
condition. How can I validate each email in the array whichever way before sending to the mail function?
1 Answer
Reset to default 3Are you aware you can pass a comma-separated string of email adresses to wp_mail
?
// Make sure your email strings only uses comma as a separator
$emails = preg_replace('~[,;\s]+~', ',', $emails);
Then just throw that string into wp_mail
’s first argument. Depending on exactly what you want to do with the emails, this approach may be sufficient.
The wp_mail
function internally relies on the PHPMailer library (unless you use a redefined wp_mail
function). PHPMailer validates the email before adding it to the recipient list. Invalid emails will be skipped.
本文标签: phpValidate emails in array using foreach
版权声明:本文标题:php - Validate emails in array using foreach 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745491727a2660632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论