0

My HTML: Full Name

<div class="row">
<div class="label"> Age </div>
<div class="input">
<input type="text" id="age" class="detail" name="age" value="" />
</div>
</div>

<div class="row">
<div class="label"> Gender </div>
<div class="input">
Male:<input type="radio" id="radio1"   name="gender" value="" />
Female:<input type="radio" id="radio2" name="gender" value="" />
</div>
</div>

<div class="row">
<div class="label"> Department </div>
<div class="select">
<select id="department" class="detail" name="department" value="" />
<option>Please select below</option>
<option>xxxx</option>
<option>xxxx</option>
</select>
</div>
</div>

<div class="row">
<div class="label"> Who do you want to consult </div>
<div class="select">
<select id="consult" class="detail" name="consult" value="" />
<option>xxxx</option>
<option>xxxx</option>
<option>xxxxy</option>
<option>xxxxx</option>
<option>xxx</option>
<option>xxxxx</option>
</select>
</div>
</div>


<div class="row">
<div class="label"> When do you want to visit us</div>
<div class="date">
<input type="date" id="date" class="detail" name="date" value="" />
</div>
</div>

<div class="row">
<div class="label"> Do you have any specific requests or needs</div>
<div class="request">
<input type="text" id="request" class="detail" name="request" value="" />
</div>
</div>

<div class="row">
<div class="label"> When is a good time to call you</div>
<div class="time">
<input type="time" id="time" class="detail" name="time" value="">
</div>
</div>


<div class="row">
<div class="label"> E-mail </div>
<div class="input">
<input type="email" id="email" class="detail" name="email" value="" />
</div>
</div>

<div class="row">
<div class="label"> Phone number </div>
<div class="input">
<input type="tel" id="tel" class="detail" name="tel" value="">
</div>
</div>


<div id="submit-reset">
<input type="submit" id="submit" name="submit" value="Submit Form" />
<input type="reset" id="reset" name="reset" value="Reset" />
</div>
</form>

I'm having hard time configuring PHP part for this My PHP code:

$fullname = $_POST['fullname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$department = $_POST['department'];
$consult = $_POST['consult'];
$date = $_POST['date'];
$request = $_POST['request'];
$time = $_POST['time'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$to = "example@email.com";
$subject = "message goes here";

mail ($to, $subject, "From: " .$fullname);
echo "message sent"
?>

When tested with my mail ID it shows that message is sent, but i am not receiving any mail at all. Please help me out on this. I ama rookie to php so i dno't know how it works at all :( Also any regarding validation would be much appreciated.

  • Is your mail server configured? mail() will call sendmail on linux/unix-ish systems. If sendmail isn't configured or mapped to some other mail engine, the mail() call can fail. See what the return value from mail() is by something like `echo "Returned from mail: " . mail($to, $subject, "From: " .$fullname)` – user49438 Nov 13 '15 at 18:07
  • The code *assumes* that the message was sent. What is the actual return value from `mail()`? Also, even if the message is sent, there are *tons* of reasons why it might not end up in your inbox which have nothing to do with your code. – David Nov 13 '15 at 18:08

3 Answers3

0

Your HTML form is missing the start form tag. make sure your html form start with form tag and method is post with action is the php mail script file name.

Ex: <form method="post" action="mail.php"> .. .. .. .. </form>

and in mail.php enter the mail script as follow

$fullname = $_POST['fullname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$department = $_POST['department'];
$consult = $_POST['consult'];
$date = $_POST['date'];
$request = $_POST['request'];
$time = $_POST['time'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$to = "example@email.com";
$subject = "message goes here";
$message="Name: ".$fullname."<br/>Age: ".$age,"<br/>Gender",$gender."<br/>Phone".$phone."<br/>Dept:".$department;
if(mail ($to, $subject,$message, "From: " .$fullname)){
    echo "Success";
} else {
    echo "not sent";
}

Also make sure that php mail server is configured properly.

If you are testing it on localhost on windows/Linux machine then you should first configure your SMTP setting and enable to send mail from localhost.

If you are testing it on live server then contact your service provider to enable sending mails

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Vidit Mody
  • 45
  • 5
0

You have not mentioned your environment but you can try to send it form command line and see if the mail goes. I am suspecting that your server is not able to use mail in php. You can try using smtp mail as an option Here is a great mail class https://github.com/PHPMailer/PHPMailer hope this helps

Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
0

I think you need to add the From as an address on your server and put a Reply-to header so that when you get the email in your inbox you can reply to it in the normal way.

As far as I can see instead of a From on your server you have their full name from the form.

To quote @nana-partykar for the message part - replace from $message onwards with:

    $message="Name: ".$fullname."<br/>Age: ".$age,"<br/>Gender",$gender."<br/>Phone".$phone."<br/>Dept:".$department;

    $your_email = 'your_email@your_server.com';
    $headers = "From: $your_email" . "\r\n";
    // Additional headers
    $headers .= "Reply-To: $email" . "\r\n";
    if(mail($to,$subject,$message,$headers)){
    echo "Success";
    } else {
    echo "not sent";
    }

http://php.net/manual/en/function.mail.php

You could also try setting the sendmail user at the top of your script:

    ini_set('sendmail_from', 'your_email@your_server.com'); 

http://php.net/manual/en/book.mail.php

As far as validation is concerned you could clean up what is submitted to allow only letters, numbers and a few other characters:

    $email = preg_replace("[^a-zA-Z0-9@._-]",'',$_POST['email']);

and at least make sure that the essential email address is not empty by wrapping the mail part with:

    if($email != ""){

    if(mail....to the end

    }

If you are putting any of the received information into a database you need to ensure it is also free of inject attack code

How can I prevent SQL injection in PHP?

I would suggest getting it working first before you do that...but do do it!

Community
  • 1
  • 1
Steve
  • 808
  • 1
  • 9
  • 14