0

I am building a website as an exercise. It has a 'contact us' page in which I have created a form. I have saved that page with .php extension, believing that in such case only, the webpage would send the form data to another php page. Once send button is pressed, it leads to a php page with html and php script which should send an email automatically. but that is not happening. here is the php script:

<?php


if(isset($_POST['submit']))

{

     $name = $_POST['name'];
     $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $country = $_POST['country'];
    $city = $_POST['city'];
    $message = $_POST['contactusmessage'];

    $headers = '[email protected]';


    $to = '[email protected]';

    $mail=mail($to,$message,$headers);

        if($mail){
            echo"<p>Thanks $name for the message.</p>";
            echo"<p>We will reply to $email</p>";
        }
        else{
            echo "Mail sending failed."; 
        }

}

?>

Please tell me where I am going wrong?

2
  • 1
    You should probably remove the e-mails you've mentioned here before they're picked up by bots Commented Jun 24, 2014 at 18:40
  • In accordance with Brian's answer, why not use the email they provide you with as the from, or even a reply-to? You may have a reason for not doing this but it just makes more sense to me as then you can reply to the email. Commented Jun 24, 2014 at 18:44

2 Answers 2

5

$headers = '[email protected]'; jumps out at me seeing as how that's an invalid header. That's likely why your mail isn't going out. Try changing it to $headers = 'From: [email protected]';

Also change your $mail=mail($to,$message,$headers); to $mail=mail($to,'This is the subject',$message,$headers); as mentioned in the other answer below. You're missing the subject parameter to mail().

Sign up to request clarification or add additional context in comments.

3 Comments

did the change... yet mail sending failed.
Register_globals is turned off and i have no control over it.
register_globals has nothing to do with the problem here ;) I missed the fact that you were also missing the subject parameter to mail() as mentioned below by @mti2935. You need both of these things to be fixed so that your header is valid and your call to mail() is correct. If that still doesn't work, please update your question with the new code base so we are seeing the latest version of it.
1

In addition to what Brian pointed out, you might also want to specify the subject of the message in the call to the mail() command, and specify the envelope sender (as well as specifying the sender in the headers). The mail() command should look something like this:

mail($to, "web form submittal", $message, $headers, "-f [email protected]");

1 Comment

I totally missed the missing subject parameter there. Nice catch ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.