KawaiKx
KawaiKx

Reputation: 9910

sending email via HTML form using php script

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?

Upvotes: 0

Views: 207

Answers (2)

Brian
Brian

Reputation: 3023

$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().

Upvotes: 5

mti2935
mti2935

Reputation: 12017

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]");

Upvotes: 1

Related Questions