PHP Email Form Thankyou Note
After much pain and heartache I now have a working PHP Email contact form. The only thing it is missing is a thank you note or message. Could anyone help me out?
<form name="email" method="post">
<p>
<label for="name">Full Name</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="email">Email Address</label>
<input type="text" name="email" id="email">
</p>
<p>
<label for="phone">Phone Number</label>
<input type="text" name="phone" id="phone">
</p>
<p>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject">
</p>
<p>
<label for="message">Message<br>
</label>
<textarea name="message" id="message" cols="45" rows="5"></textarea>
</p>
<p><input type="submit" name="send" id="send" value="Submit"><input type="reset" name="send" id="send" value="Reset">
</p>
</form>
<?php
// Get the Variables
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Validate against spammers
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return f开发者_运维知识库alse;
}
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
// Compose the Email
$email_from = 'a@b.com'; // Set a valid email address that the form can use
$email_subject = "New Form submission - $subject"; // Change the message subject here
$email_body = "You have received a new message from $name ($phone) .\n Here is the message:\n $message";
// Send The Email
$to = "a@b.com"; // Set a valid email address to send the form to
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
if(mail($to,$email_subject,$email_body,$headers)){
echo "Thanks for your mail...";
}
very simple, mail() function returns true/false so using this:
echo mail($to, $email_subject, $email_body, $headers) ? 'Mail send' : 'Failed to send mail';
will echo a result to the screen. However you might want to add the forms action ABOVE the form, since it's not very common that messages are placed underneath. Also you could use this:
if($_SERVER['REQUEST_METHOD'] == "POST" && !IsInjected($visitor_email)) {
// insert form action here
} else {
if(IsInjected($visitor_email)) {
echo "False e-mail address supplied
}
// add form HTML code here (outside PHP tags ofcourse
}
In this case you first check up if a post-request is being send (and the e-mailadres is not false), if so: perform the send action. Otherwise message the error for a false e-mail and display the form.
Ofcourse this isn't a fool proof example, you have to do some work yourself.
精彩评论