php - Validate emails in array using foreach

How do I validate an array of emails from a textbox before sending them to the wp_mail() function?$emails = '[email

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?

Share Improve this question edited May 15, 2019 at 13:22 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Jul 16, 2012 at 11:00 LoxzibitLoxzibit 1534 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Are 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745491727a4630016.html

相关推荐

  • php - Validate emails in array using foreach

    How do I validate an array of emails from a textbox before sending them to the wp_mail() function?$emails = '[email

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信