Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Make sure email field is valid?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37831
    ffxpwns
    Member

    Hey, I have some code for an “enter email to receive a newsletter” thing. The code as is works exactly how the client wants it, but I also want to implement this email validation. Preferably the ‘advanced’ one. How would I go about doing that? The code for the emailer I already have working is below. Thank you!

    [index.html]





    Sign up to receive Auction notifications










    And heres the emailer.php




    $email = trim(strip_tags($_POST));



    $subject = "Contact form submitted!";
    $to = '[email protected]';

    $body = << $message
    HTML;

    $headers = "From: $emailrn";
    $headers .= "Content-type: text/htmlrn";


    mail($to, $subject, $body, $headers);

    header('Location: thanks.html');
    ?>
    #101943
    ffxpwns
    Member

    Also, thanks.html shouldn’t matter, right?

    #101952
    seanroberts
    Member

    I did not check your code. But I below is a simple check for if the email is valid. If the validation returns true the message is created and sent. Otherwise, they are sent back to the index page.



    $email = trim(strip_tags($_POST));
    //test if email is valid
    //if it is continue, otherwise send them back to the index page
    if(isValidEmail($email)){


    $subject = "Contact form submitted!";
    $to = '[email protected]';

    $body = << $message
    HTML;

    $headers = "From: $emailrn";
    $headers .= "Content-type: text/htmlrn";


    mail($to, $subject, $body, $headers);

    header('Location: thanks.html');
    }else{

    header('Location: index.html');
    }

    function isValidEmail($email)
    {
    //Perform a basic syntax-Check
    //If this check fails, there's no need to continue
    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
    return false;
    }

    //extract host
    list($user, $host) = explode("@", $email);
    //check, if host is accessible
    if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
    {
    return false;
    }

    return true;
    }

    ?>

    hope this helps

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.