0

I want to write a script that takes about 5 email addresses and then check if they are valid and if they are then send a message to each email address. I previously wrote this but got stuck along the way because the code runs when all emails were valid. How do i individualize the emails for checking and making sure even if one of the emails is invalid the code will display the rest of the valid ones.

<form method="post" action="validate_emails.php">
<p>Enter emails and separate them with a comma.</p>
<textarea name="emails" cols="50" rows="10">

</textarea>

<p><input name="send" type="submit" value="submit"></p>
</form>
<?php

if(isset($_POST['send'])) {
    $fes = preg_split('[,\r\n]', $_POST['emails']);


    foreach ( $fes as $key => $email ) 
    {     

      if(filter_var($email, FILTER_VALIDATE_EMAIL)){
            echo $fes[key];

                    //mailing to code not entered.

      }


    } 

}

?>

1 Answer 1

2
foreach ( $fes as $key => $email ) 
    {     

      if(filter_var($email, FILTER_VALIDATE_EMAIL) == false){
            // email is invalid, do what you want
           continue;
      }

     // now email is valid 
     // code for sending email
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Would be a cool way to do it, but I get some weird behaviors. For example, an email address without a TLD, like foo@bar, will validate, but if the hostname ends in a digit, like foo@bar2, it won't validate. But IP addresses do validate for the hostname part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.