Ok I figured it out 😃. The problem was with the "if (empty($_POST['message']))".
I changed it to "(!empty(" and it works now.
Here is the whole script for send a basic email:
// check if form is submitted
if (isset($_POST['submitted'])) {
// Check for name.
if (eregi ('^[[:alnum:]\.\' \-]{2,15}$', stripslashes(trim($_POST['name'])))) {
$name = $_POST['name'];
} else {
$name = FALSE;
echo '<p class="error">Please include your name.</p>';
}
// Check for email.
if (eregi ('^[[:alnum:][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes($_POST['email']))) {
$email = $_POST['email'];
} else {
$email = FALSE;
echo '<p class="error">Please include a valid email address.</p>';
}
if (eregi('^[[:alnum:][a-z0-9_\.\-]{2,15}$', stripslashes(trim($_POST['subject'])))) {
$subject = $_POST['subject'];
} else {
$subject = FALSE;
$subject = 'I looked at your Portfolio...';
}
// Check for message
if (!empty($_POST['message'])) {
$body = htmlspecialchars($_POST['message']);
} else {
$body = FALSE;
echo '<p class="error">You did not include your message.</p>';
}
if ($name && $email && $subject && $body) { // Everythings ok.
$to = 'example@example.com';
$headers = "From: \"$name\"<$email>\n\n";
//send me the users email
mail ($to, $_POST['subject'], $body, $headers);
// Finish the page.
echo "<div style=\"position: relative; top: 20px; left: 20px; text-align: left; \">
Thank you, <strong><i> $name </i></strong><br />";
echo "Your message:<br />
<strong> $subject </strong><br />
\"<strong><font color=\"#990000\"> $body </font></strong>\"
<br />
Was sent successfully to: <br />
<a href=\"mailto: $to\">$to </a><br /><br />
</div>
";
} else { // If it did not run ok.
echo 'Your message could not sent due to a system error. <br />
I apologize for the inconvenience.';
}
} // End of the main submit conditional.
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="50%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td colspan="2" align="left"><strong>You can email me using this form.</strong></td>
</tr>
<tr>
<td align="right"> <strong>Name:</strong> </td>
<td width="72%" align="left"><input name="name" type="text" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" size="25" maxlength="30" /></td>
</tr>
<tr>
<td align="right"><strong>Email:</strong></td>
<td align="left"><input name="email" type="text" id="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" size="25" maxlength="30" /></td>
</tr>
<tr>
<td align="right"><strong>Subject:</strong></td>
<td align="left"><input name="subject" type="text" id="subject" value="<?php if (isset($_POST['subject'])) echo $_POST['subject']; ?>" size="25" maxlength="30" /></td>
</tr>
<tr>
<td align="right" valign="top"><strong>Message:</strong>:</td>
<td rowspan="2"><textarea name="message" cols="40" rows="5" id="message"></textarea></td>
</tr>
<tr>
<td align="right" valign="top"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" value="Send Message" />
<input name="submitted" type="hidden" id="submitted" value="TRUE" />
<input name="reset" type="reset" id="reset" value="Reset" /></td>
</tr>
</table>
</form>