I am using a php email form on different websites that works sporadically. Sometimes the email gets sent, sometimes it disappears into a black hole. This happens on different servers (Dreamhost, Bluehost). The emails are not showing up in spam.
I have a hidden field labeled "URL" that I use in an attempt to stop spam bots. Is it possible that's what's tripping it up? It makes no sense to me that it can work sometimes and not work other times.
This is the code:
$thebox = "<textarea name='comment' rows='9' cols='70' wrap='hard' style='background-color: #E8E8E8;'></textarea>";
$error_name = "";
$error_email = "";
$error_message = "";
if(!isset($_POST['submit'])) {
$name = "";
$email = "";
}
if(isset($_POST['submit']))
{
$url = cleanup($_POST['url']); // this field should be empty
if (!empty($url)) { exit; } // if it's not, exit the script
$name=cleanup($_POST['name']);
$email=cleanup($_POST['email']);
$message=cleanup($_POST['comment']);
$subject="Email sent via MyWebsite.com website";
$find = array("
", "
", "", "
");
$message = str_replace( $find, "<br />",$message);
$message = stripslashes($message);
$MailTo="info@MyWebsite.com";
$bad_name="Please fill in your name.";
$bad_email="Please provide a valid e-mail address.";
$bad_message="You forgot to type in a message.";
$error = 0;
$error_message="";
if ((empty($message)) || (strlen($message)<5 ) )
{
$error_message = $bad_message;
$error = 1;
}
if (!empty($message))
{
$thebox="<textarea name='comment' rows='9' cols='70' wrap='hard' style='background-color: #E8E8E8;'>".$message."</textarea>";
}
if ((!preg_match("/^([a-zA-Z0-9._])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email)) || (empty($email) ) )
{
$error = 1;
$error_email = $bad_email;
}
if (empty($name))
{
$error = 1;
$error_name = $bad_name;
}
if (!empty($name) && !empty($email) && !empty($message) && $error==0)
{
$header = "From: '$name' <$email>
" .
"Reply-To: $email
".
"Return-Path: $email
".
'X-Mailer: PHP/' . phpversion() . "
" .
"MIME-Version: 1.0
" .
"Content-Type: text/html; charset=utf-8
" .
"Content-Transfer-Encoding: 8bit
";
$Body = $message;
mail ($MailTo, $subject, $Body, $header);
echo "<p> </p> <p> </p>
<h3>
Your email has been sent.
Thank you!
</h3>
<p> </p> <p> </p><p> </p> <p> </p>";