Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

John444

macrumors member
Original poster
Feb 10, 2011
90
0
I created a contact form in html to link with a send.php file. I have no knowledge of php and everywhere I look on the internet I cannot find what I want. I can get it to send the email but it wont show any of the fields.

This is how I want the email to look:

Name: whatever they typed

Email: email@email.com

Number: 123-4567

Message: bla bla bla

Can someone please give me the php for this? (the names of the fields in the form are the same as above Name, Email, Number, Message).
 
In your php file,
use $_POST['Email'] to get the value.

Thanks for the reply!

So I use $POST['Name'] for all the values, I know that part but how do I get it to email to me. I use the mail[$to,$subject,$content] but that only gives me the subject and the message. But I want it to display the name, email, phone number and the message in the email that is sent to my email address.

any ideas?
 
Thanks for the reply!

So I use $POST['Name'] for all the values, I know that part but how do I get it to email to me. I use the mail[$to,$subject,$content] but that only gives me the subject and the message. But I want it to display the name, email, phone number and the message in the email that is sent to my email address.

any ideas?

Try this

<?php
$to = "your@email.com";
$from = "noreply@email.com";
$subject = "Your subject";
$name = $_POST['Name'];
$email = $_POST['Email'];
$number = $_POST['Number'];
$message = $_POST['Message'];
$body=
"
Name: $name
<br /><br />
Email: $email
<br /><br />
Number: $number
<br /><br />
Message: $message
<br /><br />
";
$headers = "From: $from \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
mail($to, $subject, $body, $headers);
?>
 
And if you're putting this form out in the open, prepare to be bombarded with spam messages. Also, using $_POST data without any server-side validation is a very bad idea.

Since you don't know PHP, you might be better served using one of the millions of available form-to-email PHP scripts that you can find with a quick Google search. Just about any of them will be better than what was posted above.

To get you started...
 
And if you're putting this form out in the open, prepare to be bombarded with spam messages. Also, using $_POST data without any server-side validation is a very bad idea.

Since you don't know PHP, you might be better served using one of the millions of available form-to-email PHP scripts that you can find with a quick Google search. Just about any of them will be better than what was posted above.

To get you started...

I know javascript really well, I already made a javascript validation, but I am going to do research a lot to make a php validation incase they have javascript disabled. thanks

----------

Try this

<?php
$to = "your@email.com";
$from = "noreply@email.com";
$subject = "Your subject";
$name = $_POST['Name'];
$email = $_POST['Email'];
$number = $_POST['Number'];
$message = $_POST['Message'];
$body=
"
Name: $name
<br /><br />
Email: $email
<br /><br />
Number: $number
<br /><br />
Message: $message
<br /><br />
";
$headers = "From: $from \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
mail($to, $subject, $body, $headers);
?>

Thanks, it works great, know I just have to create a php validation
 
And if you're putting this form out in the open, prepare to be bombarded with spam messages. Also, using $_POST data without any server-side validation is a very bad idea.
Just out of curiousity, how do you validate that something like this is coming from within the server?
 
Just out of curiousity, how do you validate that something like this is coming from within the server?

there is a link up above that leads to a google search, there are tons of ways to validate info using a server side script or javascript. For example
You could do something like this:

<?php
$to = "Email@email.com";
$from = " Your Website";

$test="2";

if($_POST['Name']==""){
$test="1";
}
if($_POST['Email']==""){
$test="1";
}
if($_POST['Number']==""){
$test="1";
}
if($_POST['Message']==""){
$test="1";
}

if($test=="1")
{
echo "<html><head>
<meta http-equiv=\"refresh\" content=\"10;url=contact.html\"/>
</head>
<body bgcolor=\"black\">
<font color=\"red\"><h1>There was an Error, Please ensure you filled in all the fields!</h1></font>
</body></html>
";
}
else{

$subject = "Website Submission";
$name = $_POST['Name'];
$email = $_POST['Email'];
$number = $_POST['Number'];
$message = $_POST['Message'];
$body=
"
Name: $name\n
Email: $email\n
Number: $number\n
Message: $message

";

$headers = "From: $from \n";





mail($to, $subject, $body, $headers);


echo "<html>
<head>
<meta http-equiv=\"refresh\" content=\"5;url=index.html\"/>
</head>


<body bgcolor=\"black\">

<font color=\"white\">Thank You! Your form was sent successfully</font>

</body>


</html>
";


}
?>

But this is just simple verification in PHP, I don't have much knowledge of PHP so this is the best I can do. With javascript you have a ton of ways you can verify it in great detail, and have it validate instantly on the same page they filled out the form. So I would only use this verification as a backup incase they have javascript disabled.
 
Last edited:
follow these steps to create a web form to email you:

1: Create a html or php file and code the html form into it.
2: Use javascript functions to validate all the inputs before submitting the form.
3: In the php file use a suitable mail function(download code from the internet to get saved from the trouble) to mail the contents to yourself.

Also don't forget to configure your mail server when doing that.
Either do all of the above or....download some php mail sample to make things going.
 
For validating contact forms, email injection is one of the biggest issues you want to validate on. Your form can be turned into a spam propagation tool very easily. I highly suggest you read the linked page so you can do some minimal validation. The validation you provided here doesn't do much of anything to block spam.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.