2

I'm trying to check if many e-mail addresses are correct in order to send them.

The thing is, while I filter with filter_var_array() and FILTER_VALIDATE_EMAIL, if only one is correct among all the mails, it still proceeds.

Here is my code:

$test_email_friend = explode(",", $email_friend);
if ( !filter_var_array($test_email_friend, FILTER_VALIDATE_EMAIL)) {
    $errenvoi = "Please send only valid emails.";
} 
else {  
    //message, headers etc here
    if (mail($email_friend,$sujet,$message,$entete)){               
            $errenvoi = "Email sent !";
    } 
    else {
        $errenvoi = "Something very wrong happened, abandonship, I reapeat abandonship";            
    } 
}

For example: If the array contain "test@test.com" and "unvalidmess". It's sent anyway, because one of the value is correct.

How can I fix this?

Thanks a lot

4

1 回答 1

2

如果您阅读文档,它会说此函数仅false在失败时返回(该函数无法执行)。否则,它返回一个数组。所以下面的例子:

$test = array('test@test.com', 'unvalidness');
var_dump(filter_var_array($test, FILTER_VALIDATE_EMAIL));

将输出:

array(2) {
  [0]=>
  string(13) "test@test.com"
  [1]=>
  bool(false)
}

您可以通过检查失败和在返回的数组中搜索任何布尔值来更改代码以使其正常工作false

$result = filter_var_array($test, FILTER_VALIDATE_EMAIL);
if (!$result || in_array(false, $result, true)) {
    echo 'failed or data not valid';
}
于 2016-12-02T13:56:37.743 回答