零星的php表单邮件

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> &nbsp; </p> <p> &nbsp; </p>
                <h3>
                Your email has been sent.
                Thank you!
                </h3>
                <p> &nbsp; </p> <p> &nbsp; </p><p> &nbsp; </p> <p> &nbsp; </p>";

I'm answering my own question in case anyone else runs into this issue. The problem was indeed on the server side with both hosts. As part of an effort to stop email spoofing, they look at the "From" field.

In Dreamhost's case, they do not allow the "From" field to be coming from anywhere other than a Dreamhost server. With Bluehost, it has to be a valid email address that they are able to trace back. (I used a made-up email address in my tests.)

The work-around I used in both cases was to not use the visitor's email address in the "From" field. I am using my own email address in that field and putting the visitor's email address in the "Reply To" field instead.

I already have a cleanup function to check any data that's returned. In addition, I added another layer of protection to make sure nothing is inserted into the header fields using this code:

$email= urldecode($email);
if (eregi("(|
)", $email)) {exit();}

I hope this information is of some help.