views:

18

answers:

2

I'm using a PHP contact form and it is sending mail to non gmail addresses, however when I set it to send to a gmail address, it doesn't get delivered (it doesn't even appear in junk mail).

I've heard of issues like this before - I'm not a web developer/expert so can anybody suggest code/configuration changes to my PHP contact form below which would essentially mean messages get delivered to gmail addresses?

I'm on a linux/WHM dedicated server.

<?php
error_reporting(E_NOTICE);

function valid_email($str)
{
    return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}

if(!empty($_POST['name']) && !empty($_POST['email']) && valid_email($_POST['email']) === true && !empty($_POST['comment']))
{


    $to = "[email protected]";
    $headers =     'From: '.$_POST['email'].''. "\r\n" .
            'Reply-To: '.$_POST['email'].'' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
    $subject = "Contact Form";
    $message = htmlspecialchars($_POST['comment']);

    if(mail($to, $subject, $message, $headers))
    {
        echo 1; //SUCCESS
    }
    else {
        echo 2; //FAILURE - server failure
    }
}
else {
    echo 3; //FAILURE - not valid email
}
?> 
A: 

Did you check to see if your PHP installation/server supports the Mail() function?

Also did you check to see if Gmail is treating your emails as spam? (they wont show up in the spam folder, they are just blocked)... Try sending it to a different address.

tcables
I did send to a different address and it works - so this is what I want to resolve.
samengland
A: 

Set your script to send it from an email address that has an actual mailbox somewhere so you can check for bounces. If your script echoing 1, and can send to other addresses it suggests (to me at least), that it's gmail not liking you, rather than anything wrong with the script per se.

It also looks like you need to look into protecting this script from email injection. Just a suggestion though.

boodle
How can I go about protecting this script from email injection?
samengland