0

I've made a page which will receive 3 variables and send email according to the data being sent to that page. But when I send the data from a form I get HTTP ERROR 500 when POST is used. The same code is working when GET Method is used and when data is sent in the url. What could be the mistake?

<!doctype html>
<html>
<head>
<title>MAIL</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<?php


 $emailTo=$_POST['to_address'];
 $subject=$_POST['subject'];
 $body="$_POST['body'];
 $headers="From: myemail@mysite.com";

 if (mail($emailTo, $subject, $body, $headers)) {

 echo "Mail sent successfully!";

 } else {

 echo "Mail not sent!";

 }


?>
</div>
</body>
</html>

2 Answers2

0

500 means internal Server-Error. Mostly there is a mistake in the source code.

Switch

$body="$_POST['body'];

To

$body=$_POST['body'];

You should really really escape your parameters for the mail function. Have a look at this answer:

escape mail fucntion

Community
  • 1
  • 1
osanger
  • 2,276
  • 3
  • 28
  • 35
0

you're having a typo here -> $body="$_POST['body']; /* double quotes not closed */ plus : you should really check the POST data and sanitize as it's not secured at all

OldPadawan
  • 1,247
  • 3
  • 16
  • 25
  • THE CODE IS WORKING. THANK YOU SO MUCH. How to secure the code? Any example? I'm sorry. I'm a newbie. –  Apr 01 '17 at 07:52
  • you have many posts about this, starting on [SO - question](http://stackoverflow.com/questions/18431250/how-to-secure-a-form-which-sends-out-emails) -> please read more after search :) and accept one of the two answers also please, it'll be closed – OldPadawan Apr 01 '17 at 07:56