-2

Mail is sent but the values of the form fields are not sent.

I am using the following code.

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Thank you for contact us. As early as possible  we will   contact you '
);

$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email'])); 
$subject = @trim(stripslashes($_POST['subject'])); 
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'some@email.com';//replace with your email

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

$success = @mail($email_to, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die;
?>
Xorifelse
  • 7,408
  • 1
  • 23
  • 37
GPalma
  • 1
  • 1

1 Answers1

0

Your codeflow is incorrect. Your $status is successful without doing any validation. Also we're missing a HTML form in your question which is the reason why it doesn't work.

<?php
  header('Content-type: application/json');

  if(
      isset($_POST['name']) &&
      isset($_POST['email']) &&
      isset($_POST['subject']) &&
      isset($_POST['message']))
  {
    # Here you should do some actual validation..
    #  1. is email address valid?
    if(!filter_var($email, FILTER_VALIDATE_EMAIL) === false){
      # The email has a correct format
      // etc, etc, etc.
    } else {
      $status = [
      'type' => 'failed',
      'message' => 'Incorrect email address'
      ];
    }
  } else {
    $status = [
    'type' => 'failed',
    'message' => 'Missing fields: ' . var_export($_POST) . ' But wait a minute...: ' . var_export($_GET)
    ];
  }

  die(json_encode($status));

?>

And for the HTML:

<form method="post" action="yourphpfile.php">
  <input type="text" name="name" value="">
  <input type="text" name="email" value="">
  <input type="text" name="subject" value="">
  <input type="text" name="message" value="">
  <input type="submit" value="Submit">
</form>

Why are you using @ to suppress error messages?

  1. Error messages are useful to debug code with. (yet you suppress them and ask for help)
  2. Using them is a bad practice in PHP programming.
  3. Useless before the trim function, it is expected to work, always.
Xorifelse
  • 7,408
  • 1
  • 23
  • 37
  • How does that explain why the email arrives with no form data? – j08691 Mar 09 '16 at 21:01
  • Running this code will result in a json response, and will export the `$_POST` data showing his HTML form is not setup correctly. – Xorifelse Mar 09 '16 at 21:04
  • 1
    You wrote an entirely new script, but didn't answer *why* the OP's code sends email without form data. Without more information from the OP, it's pretty much impossible for anyone to do anything but hazard a guess. – j08691 Mar 09 '16 at 21:05