WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I validate an array of emails from a textbox before sending them to the wp_mail function?

$emails = 'test@test.com;test2@test2.com
email3@email3.com,email4@email4.com, email5.com';

$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 kinda sample code above stops on the if (is_email()) condition. Was wondering how I can validate each email in the array whichever way before sending to the mail function.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
I was aware that you could use comma-seperated string but because i was using explode function to get emails from text box, i was running into the problems mentioned above. Thanks very much, much appreciated Geert. – Loxzibit Jul 16 '12 at 13:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.