For some reason the $name, $email and $message variables aren't being passed into the $msgcontents and $headers variables. After I fill out the form and click submit the message in my inbox looks like this:
Subject: Message from Contact Form
From: Unknown sender
Date: Mon, November 17, 2014 7:44 pm
To: myemail@myemail.com
Name:
Email:
Message:
I've used var_dump() to see if my variables are being populated after clicking submit and they are but for some reason they are not being picked up by $msgcontents and $headers. Where am I going wrong?
Here's my code:
<?php
$name = trim(htmlspecialchars($_POST['name'], ENT_QUOTES));
$email = trim($_POST['email']);
$message = trim(htmlspecialchars($_POST['message'], ENT_QUOTES));
$to = "myemail@myemail.com";
$subject = "Message From Contact Form";
$msgcontents = "Name: $name <br> Email: $email <br> Message: $message";
$headers = array("MIME-VERSION: 1.0",
"Content-type: text/html; charset=iso-8859-1",
"From: $name <$email>",
"Reply-To: contact@info84.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("
", $headers);
$mailsent = mail($to, $subject, $msgcontents, $headers);
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="contactform" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" novalidate>
<input type="text" name="name" placeholder="Your Name">
<input type="email" name="email" placeholder="Your Email">
<textarea name="message" placeholder="Your Message"></textarea>
<input type="submit" name="submitform" value="send">
</form>
<?php
var_dump($to);
?>
<br>
<?php
var_dump($subject);
?>
<br>
<?php
var_dump($msgcontents);
?>
<br>
<?php
var_dump($headers);
?>
<br>
<?php
var_dump($name);
?>
<br>
<?php
var_dump($email);
?>
<br>
<?php
var_dump($message);
?>
</body>
</html>
I think you check the message sended from first page load (without the form)
Try to wrap your email sending code, like this:
if (isset($_POST['submitform'])) {
//...
mail(...);
//...
}
Then, when page loads no message been sended, but when you click a submit button, mail been sended