Reputation: 15
The goal of this code is to have someone fill it out then it sends their typed message to my email. I've looked online for some references, and being so new to php I'm having a lot of trouble understanding why when I click the send button it shows my php pages code. My question is why am I not getting an email when pressing the send button on the contact form?
<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
</select>
<br />
<p>Type</p>
<select name="type" size="1">
<option value="update">Question</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">Misc.</option>
</select>
<br />
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text- decoration:none;color:#ff0099;'> Return Home</a>";
?>
All help is appreciated. Thanks!
Upvotes: 0
Views: 73
Reputation: 3013
You also have a very common error a lot of people have with "Contact Us" forms.
$email = $_POST['email'];
$mailheader = "From: $email \r\n";
This will break SPF and also cause DMARC to fail and you will never get the message from some people, if your mail server you use has DMARC enabled on it and GMAIL does.
Since DMARC is a more recent protocol, a lot of the old cookie cutter code for contact us forms - doesn't take this into account.
You can read more about that here: "DMARC - Contact Us Form Nightmare"
The suggested workaround will be to do:
$email = "[email protected]";
$subject = "Contact Form Email: " . $_POST['email'];
This way - you avoid the issue outline in the article. You won't quickly be able to hit the "Reply" button, but at least you'll get the emails from those customers who have DMARC enabled.
Upvotes: 0
Reputation: 3187
Set you action=""
so that the form is sent to the page itself. Execute Php code only if form is being posted, for that i added a name
to you submit button and in Php checked if submit exists in %_POST
array telling Php when to execute this code. Since you'll be at the same page so i changed text of anchor tag.
Note: This code assumes that is being executed on server where email server is configured and no error occurs during execution
<form action="" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
</select>
<br />
<p>Type</p>
<select name="type" size="1">
<option value="update">Question</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">Misc.</option>
</select>
<br />
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" name="sendemail" value="Send"><input type="reset" value="Clear">
</form>
<?php
if(isset($_POST["sendemail"]))
{
$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text- decoration:none;color:#ff0099;'> Go Somewhere you are already home ;)</a>";
}
?>
Upvotes: 1