1

i try to send a mail with php and axios. i store my data on some properties and then try to call a post method via axios. EMAIL WITH BE SENT but with empty values. looks like i cannot get this values in php.

// js  
axios
      .post("./vendor/sendmail.php", {
        name: nameInput.value,
        email: emailInput.value,
        message: textarea.value
      })
      .then(respond => {
        console.log(respond);
      })
      .catch(error => {
        console.log(error);
      });

// php
<?php
    $email = $_POST['email'];
    $message = $_POST['message'];
    $name = $_POST['name'];
    $body = "Email: {$email}\n\nName: {$name}\n\nMessage: {$message}\n\n";
    mail("myemail@gmail.com", 'A new message', $body, "From: test@gmail.com");
?>

i get this in my inbox:

Email:

Name:

Message:

(an email with empty values)

Amir Meimari
  • 520
  • 5
  • 20
  • Watch the Network tab in your browser console, click on the request, then scroll down in Headers, to verify what is being sent. Also try `var_dump($_REQUEST);` to see what the PHP file is receiving. – aynber Jul 15 '19 at 14:10
  • @aynbe it is an empty array: `array(0) {}` – Amir Meimari Jul 15 '19 at 18:30
  • Did you check the Network tab in the browser to make sure the post values are being sent? And is the request being redirected anywhere? – aynber Jul 15 '19 at 18:34
  • Yeah i checked request in network tab in Request Payload section. all my values are there in a object. and it's not redirecting. Request URL is the php file. – Amir Meimari Jul 15 '19 at 18:38

1 Answers1

1

Axios is probably encoding it as JSON. You would need to read the input into PHP and not the $_POST variables. Give me a min, going to dig up some code.


What it does is detect the Content-Type header and if it's application/json then it will read the input into PHP and decode it. It could use a try/catch, just in case of invalid JSON.

function convertInput() {
  if (!empty($_SERVER["HTTP_CONTENT_TYPE"])) {
    $contentTypes = explode(";", $_SERVER["HTTP_CONTENT_TYPE"]);

    if (in_array("application/json", $contentTypes)) {
      $rawInput = file_get_contents('php://input');
      return json_decode($rawInput, true);
    }
  }

  return [];
}
Vitaliy Isikov
  • 3,647
  • 9
  • 38
  • 46
  • 1
    Modifying superglobals is a really [bad practice](https://stackoverflow.com/a/3498957/10875622), as you overwrite all data already in `$_POST`. Also, why using getallheaders()? Content-Type header is available in `$_SERVER['HTTP_CONTENT_TYPE']`. – Maxime Launois Jul 15 '19 at 14:34
  • 1
    Sure, I agree with you. This was ripped from a project I created close to 5 maybe 7 years ago. I've edited my answer. – Vitaliy Isikov Jul 15 '19 at 14:45
  • Thank you for the answer but i didn't understand. are you saying that the values i'm sending with axios are correct or something? – Amir Meimari Jul 15 '19 at 18:39
  • i tried `$data = json_decode(file_get_contents('php://input'), true);` and it worked! – Amir Meimari Jul 15 '19 at 19:10