phpvalidationemailfilter-var

filter_var : validate array of email addresses


I have a dropdown list where when the user choose All, it will sent email to all recipients that I get from the database. If the user choose 'Other', a textfield will appear which the user have to enter the email address. I would like to validate email that the user entered in the 'Other' textfield is valid. I want to allow user to be able type multiple email addresses, which separated by commas.

This is my form:

<tr>
  <td>To</td>
  <td>:</td>
  <td>
    <select name="email" id="email" onchange="if(this.value == 'Other') {this.form['Other'].style.visibility='visible'}else{this.form['Other'].style.visibility='hidden'};">
      <option value=""></option>
      <option value="All">All</option>
      <option value="Other">Other</option>
    </select>
    <?php if(isset($error[ 'email'])) echo $error[ 'email']; ?>
    <input type="text" id="Other" name="Other" style="visibility:hidden;" size="46" placeholder="Enter e-mail separated with commas" />
    <?php if(isset($error[ 'Other'])) echo $error[ 'Other']; ?>
  </td>
</tr>

This is my validation for email:

if($_POST['Other'] != "" && (!filter_var($_POST['Other'], FILTER_VALIDATE_EMAIL)))
{
	$error['Other'] = "<div class=error>Email entered is not valid.</div>";
}	

I use FILTER_VALIDATE_EMAIL to validate email addresses. My problem is, If entered one email address, there is no problem, the message will be sent and I received it. But if i entered more than one email, the validation error message will prompt me that the email address I entered is not valid. Can I know why it only works for one email but not for multiple email addresses?


Solution

  • Something like this you need to do,

    $emails = 'bill@microsoft.com, test.com, obama@usa.gov';
    $emails = explode(', ', $email);
    $filterd_emails = array();
    foreach($emails as $email){
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $error['Other'][] = "<div class=error>$email is not valid Email.</div>";
        }else{
            $filtere_emails[] = $email
        }
    }
    

    EDIT

    Now you've all validated emails stored in $filtered_emails. Here are two different scenarios.

    1) You want to send emails to all filtered_emails (that pass the criteria), no matter how many pass the criteria.

    //$emails = 'bill@microsoft.com, test.com, obama@usa.gov';
    $filtered_emails = array('bill@microsoft.com', 'obama@usa.gov');
    

    for that you can use following approach, it'll skip test.com and will send email to other two

    if(count($filtered_emails) > 0)
    foreach($filtered_emails as $filtered_email){
        //send email here. email($filtered_email);
    }
    

    2) you don't want to send email any person if any of input emails failed to pass validation.

    e.g. test.com has failed to pass the criteria, and you don't want to send email at all(to other two bill@microsoft.com and obama@usa.gov)

    in this case,

    if(count($error['other'])>0){
        //display error and exit
    }
    

    it's now totally up to you which technique you want to use.