Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ammu Nair
Ammu Nair
9,100 Points

Validate email method in phpMailer library always returning false.

Hi,

Im using the PHPMailer Library to send email. Suggest.php Code below.

<?php 
//Form is submitted to the same page
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_EMAIL));
    $details = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_SPECIAL_CHARS));
//Validation
    if($name == "" OR $email == "" OR $details == ""){
        echo "Please fill in the required fields: Name, Email, Message";
        exit;
    }
    if($_POST["address"]!=""){
        echo "Bad input";
        exit;
    }
    require("inc/PhpMailer/class.phpmailer.php");
    $mail = new PHPMailer;
    //Validate email
    if(!$mail->validateAddress($email)){
        echo "Invalid Email Address";
        exit;
    }
Ammu Nair
Ammu Nair
9,100 Points

Even on entering valid email in the form, Im getting error 'Invalid Email'. Please help. Thanks.

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Ammu Nair,

It would appear the names you've given for email and details are the same as your name field, you simply need to change the string to reflect your other fields as shown below.

$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

Happy coding!

Ammu Nair
Ammu Nair
9,100 Points

Did not notice that. Thank you so much Chris. Cheers, Ammu