treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Make sure email field is valid?

  • 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]

    <body>
    <div id="main">
    <div id="contact">
    <h1>Sign up to receive Auction notifications</h1>
    <form action="emailer.php" method="post">
    <input type="text" name="email" id="email" placeholder="yourname@email.com" />
    <input type="image" name="submit" src="images/submit.jpg" width="146px" height="20px" value="Submit">
    </form>

    </div>
    </div>
    </body>

    And heres the emailer.php

    <?php


    $email = trim(strip_tags($_POST['email']));



    $subject = "Contact form submitted!";
    $to = 'BlankingOutMyEmail@mail.com';

    $body = <<<HTML
    $message
    HTML;

    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";


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

    header('Location: thanks.html');
    ?>
  • Also, thanks.html shouldn't matter, right?
  • 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.


    <?php

    $email = trim(strip_tags($_POST['email']));
    //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 = 'BlankingOutMyEmail@mail.com';

    $body = <<<HTML
    $message
    HTML;

    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";


    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
  • well, I would think it would work like so



    <?php
    $email= trim(strip_tags($_POST['email']));
    if (isValidEmail($email))
    {
    $subject = "Contact form submitted!"; $to = 'BlankingOutMyEmail@mail.com'; $body = <<<HTML$messageHTML; $headers = "From: $email\r\n"; $headers .= "Content-type: text/html\r\n"; mail($to, $subject, $body, $headers); header('Location: thanks.html');
    }
    else
    {
    //make an error page in case it fails
    header('Location: error.html');
    }

    //Check-Function
    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;
    }
    ?>