0

I am creating a contact us form, In that i am using HTML and PHP .Now my doubt is after click the submit button the details are not sending to mail,I want to know where i am mistaken on my code. Here my HTML code for create the form

<form action="sendmail.php" method="post" >
   <table width="100%" border="0"align="center"cellpadding="3"cellspacing="1">
      <tr>
         <td width="10%">Subject</td>
         <td width="2%">: </td>
         <td width="82%"><input name="name" type="text" id="name" size="50"></td>
      </tr>
      <tr>
         <td>Detail</td>
         <td width="2%">: </td>
         <td width="82%"><textarea name="detail" cols="50" rows="4"id="detail"></textarea></td>
      </tr>
      <tr>
         <td>Email</td>
         <td>: </td>
         <td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
      </tr>
      <tr>
         <td>Name</td>
         <td>: </td>
         <td><input name="name" type="text" id="name" size="50"></td>
      </tr>
      <tr>
         <td><input type="submit" name="send" value="Submit">
            <input type="reset" name="submit" value="Reset">
         </td>
      </tr>
   </table>
</form>

PHP code is below:

<?php 
  $subject="$subject";
  $message="$detail";
    $mail_from="customer_mail";
//From
$header="from: $name <$mail_from>";
//Enter your email address
$to="simon@abcinfomedia.in";
$sendmail=mail($to,$subject,$message,$header);
//check mail send to ur mail
if($sendmail){
    echo"success";
}
else{
    echo"Error";
}
?>
Santosh Ram Kunjir
  • 1,074
  • 1
  • 12
  • 23

2 Answers2

1

please try this:

<?php
if(isset($_POST['send']) && !empty($_POST['send'])) {
$subject = $_POST['subject'];//if you sent by post method else put manually
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
//Enter your email address
$to = "simon@abcinfomedia.in";
$sendmail = mail($to, $subject, $message, $header);
//check mail send to ur mail
if ($sendmail) {
    echo "success";
} else {
    echo "Error";
}
}

?>
Mayank Awasthi
  • 491
  • 2
  • 9
0

This is not working because you're not filling the variables with the data from the form by using $_POST[''].

The HTML doesn't change, but try this PHP:

<?php
$subject   = $_POST['subject'];
$message   = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header    = "from: $name <$mail_from>";
$to        = "simon@abcinfomedia.in";

//check mail send to ur mail
if(mail($to,$subject,$message,$header)){
    echo"success";
}
else{
    echo"Error";
}
?>

I have defined subject, message and mail_from using the $_POST data from the form. For the if statement, I have dropped the part where you store the mail() function as a variable and just put it straight into the if.

This will simultaneously send the email and check it at the same time.

In my test, the email worked but there were flaws because some of your fields are missing and not defined, but I believe the above solves your initial problem. :)

Tom Cash
  • 166
  • 6