I’ve uploaded the form below, to a webpage at mydomain.com/submit.php. When I visit the webpage, add some text and click the “Send” button, I don’t receive an email for the form submission. What else do I need to add to make the form work?
I understand that the form needs some security, but for the moment I just want to get the form to email me.
<html>
<form action="submit.php" method="post">
<input type="text" id="message" name="message" />
<input type="submit" value="Send" name="submit" />
</form>
<?php
$to = "myemailaddress@gmail.com"; // the actual form has my real email address here
$subject = "Form Submission";
$msg = $_POST['message'];
mail($to, $subject, $msg);
?>
</html>
I would check with your host I know on some hosts require a send mail path which is -f before a local email or a valid from and reply header all of this can be found here http://www.php.net/manual/en/function.mail.php
I think the main issue is that you are getting an undefined index error as the POST key “message” is not set or sent until after POST. The script should only be run after POST so wrap code in an IF condition saying that if is set POST submit. I would also run this code before headers are sent, meaning something sent to browser(html etc). Don’t forget the body tags…
<?php
if(isset($_POST['submit'])){
$to = "myemailaddress@gmail.com"; // the actual form has my real email address here
$subject = "Form Submission";
$msg = $_POST['message'];
mail($to, $subject, $msg);
}
?>
<html>
<body>
<form action="submit.php" method="post">
<input type="text" id="message" name="message" />
<input type="submit" value="Send" name="submit" />
</form>
</body>
</html>
You would also need to add validation for the message field prior to copying it out of the $_POST array as otherwise there is nothing to prevent someone adding a series of CC: headers to the message so as to use your form to send out their spam.