Helpful Information
Category: HTML FORM
HTML Form -> PHP mail (FAQ tutorial link broken)


I'm having difficulty wrapping my mind over PHP syntax and I have a project that should already be online but it all hinges on getting the form to work. It's very simple, but for some reason all the scripts I've found and tried fail to work when I modify them and I don't understand why.

I need to send $name, $email, $phone and $message. I don't know why this is so difficult.

I've managed to get it to send me emails, but they're always blank. I have jQuery scripts and Google Analytics in the HTML but I don't know why that would cause any problems. Below is the code for the form, below that my <head> code:

<form id="inputArea" action="sendmail.php" method="post" enctype="text/plain">

<p class="first">
<label for="name"></label>
<input type="text" name="name" id="name" size="30" onFocus="clearText(this)" value="Your Name" />
<label for="email"></label>
<input type="text" name="email" id="email" size="30" onFocus="clearText(this)" value="Your Email" />
<label for="phone"></label>
<input type="text" name="phone" id="phone" size="30" onFocus="clearText(this)" value="Your Phone Number" />
<label for="message"></label>
<textarea name="message" id="message" rows="12" cols="30" onFocus="clearText(this)">Your Message</textarea>

<p class="submit"><button type="submit">Send</button></p>


<meta name="robots" content="index, follow" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="mytext" />
<meta name="keywords" content="mytext" />
<link REL="SHORTCUT ICON" HREF="favicon.ico">
<script type="text/javascript" src=""></script>
<script type="text/javascript" src="js/slimbox2.js"></script>
<script type="text/javascript" src="js/easySlider1.5.js"></script>
<script type="text/javascript" src="js/scroll.js"></script>
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12630870-1");
} catch(err) {}
<script type="text/javascript">
<script type="text/javascript">
function param() {
if(!("search" in location))return null;
if(!q)return null;
if(q.charAt(0)!="?")return null;
p[(decodeURI(n[i][0])).replace(/\+/g," ")]=(decodeURI(n[i][1])).replace(/\+/g," ");
pn[pn.length]=(decodeURI(n[i][0])).replace(/\+/g," ");
if(l==0)return pn;

if(arguments[i]+1==arguments[i]-0+1){if(arguments[i] in pn){n[n.length]=pn[arguments[i]];continue}}
if(arguments[i] in p){n[n.length]=p[arguments[i]];continue}
return n;

<script type="text/javascript">


<link href="printstyles.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/slimbox2.css" type="text/css" media="screen" />

You forgot to post the form handler code (which appears to be sendmail.php). Also, "FAQ tutorial link broken"? Your post mentions nothing about that.

Sorry for forgetting to mention that, my post was slightly rushed. The FAQ that new posters are asked to read before had a post about PHP Mail forms, but the tutorial it links to is broken and didn't do anything to clarify the situation.

I've managed to get the form mailer to mail me with info now, but for some reason it complained that I had too many variables (6), so I cut out the subject and now the subject is the name field, and in the mail body I only receive the phone and email address and not the message.

<head><title>PHP Mail Sender</title></head>

/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$to = "[email protected]";
$subject = "Sweetness Form";
$name = $HTTP_POST_VARS['name'];
$phone = $HTTP_POST_VARS['phone'];
$email = $HTTP_POST_VARS['email'];
$message = $HTTP_POST_VARS['message'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if ($message == "") {
echo "<h4>You forgot to write your message!</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($to,$name,$phone,$email,$message)) {
echo "<h4>Thank you for sending email</h4>";
echo "<a href='javascript:history.back(1);'>Return to Sweetness</a>";
} else {
echo "<h4>Can't send email to $email</h4>";

Unless you have a special purpose for doing otherwise, you should use $_POST in place of $HTTP_POST_VARS which is deprecated. $_POST has been available since PHP 4.1 according to the documentation.

Regarding the mail function itself, you can't just throw arguments at it and expect it to handle them automatically. Same for any function. There is a specific order of arguments as described on the manual page ( That documentation and the examples should help.

Nevermind, I got it to work. Thanks again for the link to the documentation!

I'm surprised you get a message at all. The fourth argument is for email headers, not email body content as you've done above - they are different. You should rather concatenate the posted message, name, and phone number into a single variable containing the message body and use that as the third argument.