PHP Contact form editing
Im not a pro at PHP, just starting actually and was wondering if you could help me.
Im trying to get this contact form to email me stating the Persons Name and t开发者_如何学运维he message like this.
Name: Fred Blogs Message: Message, Message.
But when I try all I get is the message, I cant seem to insert the name variable anywhere.
This is the code
<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
mail( "keiron.lowe@example.com", "Name: $name", "$subject", $message, "From: $email" );
header( "Location:contact.php" );
?>
You've got the arguments mixed up a little:
mail( "keiron.lowe@gmail.com", $subject, "Name: $name\nMessage: $message", "From: $email" );
Additionally you shouldn't do "From: $email"
without validating the email address - this will leave your script open to sending out spam.
You are passing too many parameters to the mail()
function. Try something like this:
<?php
ob_start();
$name = $_POST['name'] ;
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$message = $_POST['message'] ;
$subject = strtr($_POST['subject'],array(':' => ' ', "\r" => ' ', "\n" => ' '));
$message = "Name: {$name}\r\nmessage: {$message}";
mail("redacted@example.com", $subject, $message, "From: {$email}");
header("Location: contact.php", true, 302);
ob_end_clean();
die;
?>
Take a look at the manual for mail():
mail("keiron.lowe@gmail.com", "Name: $name", $message, "From: $email");
But anyways, I strongly suggest you don't rely on PHP's mail()-function as its return value does not indicate if a mail really has been sent. Use phpmailer for mailing instead.
Best wishes,
Fabian
精彩评论