-3

I know that there is a ton of solutions to this problem but i need to validate email in a contact form.

this is my php mailer so far:

<?php
if (isset($_POST['send'])){ 
     $to = "mail@mail.something"; 
     $subject = "new message"; 
     $firstname = $_POST['name'];
     $email = $_POST['email'];
     $message = $_POST['message']; 
     $headers = "From: $email"; 
     $sent = mail($to, $subject, $message, $headers) ; 
     if($sent) 
     {echo "<script>alert('thanks for the message:) ');</script>"; 
    }else
        {echo "<script>alert('sorry, message wasn't send');</script>"; }
    }
?>

I have tried with filter_var

$result = filter_var( 'something@something.something', FILTER_VALIDATE_EMAIL );

but, doesn't work.

If my way to sent mail through contact form isn't the "best practice way" feel free to correct me and send me on the right path :D

1 Answers1

0

If you show us your HTML we can help further, but with just your PHP we don't have much to work with.

Add error_reporting(E_ALL); and ini_set('display_errors', 1); to the top of your php page to print your errors.

I am not sure what is causing your errors. But i would like you to try the following code. Replace your if statement with this.

Future note, to add a javascript alert in php you should format it this way,

if(mail($to, $subject, $message, $headers))
{
    echo '<script language="javascript">'; //echo open tag
    echo 'alert("Your message has been sent")'; // echo alert
    echo '</script>'; //echo close tag
}else{
    echo '<script language="javascript">';
    echo 'alert("Your message was not successful")';
    echo '</script>'; 
}
  • Your welcome, try replacing your whole if statement and mail send to the bottom php code on this answer. If you have any errors please post back. – Adam Joseph Looze Jan 26 '15 at 05:37
  • If this doesnt work for your needs, check out the top answer here http://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php – Adam Joseph Looze Jan 26 '15 at 05:46