Reputation: 345
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 the 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( "[email protected]", "Name: $name", "$subject", $message, "From: $email" );
header( "Location:contact.php" );
?>
Upvotes: 0
Views: 1263
Reputation: 10087
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("[email protected]", $subject, $message, "From: {$email}");
header("Location: contact.php", true, 302);
ob_end_clean();
die;
?>
Upvotes: 1
Reputation: 321766
You've got the arguments mixed up a little:
mail( "[email protected]", $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.
Upvotes: 2
Reputation: 34234
Take a look at the manual for mail():
mail("[email protected]", "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
Upvotes: 1