简体   繁体   中英

Why is my PHP contact form is sending emails with the body (message) blank?

I am a complete PHP newbie. I have a contact form which is sending me the email upon submitting the form, but the body of the email is completely empty. Everything I've tried or looked up/researched online has not worked. I have no idea what is wrong with my code. As I mentioned before, I have no idea what I'm doing with this PHP (HTML and CSS are about all I can handle). I apologize in advance, I need a serious amount of handholding to fix this! If someone could help me with this I would be eternally grateful. Please and thank you! My code is pasted below, I've changed the email address's for security reasons. Also, we are running the latest version of php on our web server's as well, if that helps.

My HTML:

<table>
<tr>
    <td valign="top" align="left">
        <form action="BRF-sendForm.php" method="post" enctype="x-www-form-encoded">
            <table width="647" border="0" align="center" cellpadding="10" cellspacing="0">
                <tr align="middle" valign="middle">
                    <td colspan="2" style="text-align: center; font-size: 20px;"><strong>Biography Request Form</strong></td>
                </tr>
                <tr align="left" valign="middle">
                    <td width="40%">*First Name</td>
                    <td width="60%"><input name="FirstName" id="FirstName" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Middle Name<br />
                        (If you would like it to appear)</td>
                    <td><input name="MiddleName" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>*Last Name</td>
                    <td><input name="LastName" id="LastName" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>*Email</td>
                    <td><input name="Email" id="Email" type="text" /></td>
                </tr>
                <tr>
                <tr align="left" valign="middle">
                    <td>Title</td>
                    <td><input name="Title" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Organization</td>
                    <td><input name="Organization" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Years in Current Role</td>
                    <td><input name="Years in Current Role" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Role/Responsibilities</td>
                    <td><textarea rows="4" name="Role/Responsibilities" />
                        </textarea>
                        </textarea>
                        </textarea></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Credentials</td>
                    <td><input name="Credentials" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Years Experience</td>
                    <td><input name="Years Experience" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>City and State of Residence</td>
                    <td><input name="City and State of Residence" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Notable Companies of Previous Employment<br />
                        (Include title(s) held)</td>
                    <td><textarea  rows="4" name="Notable Employment" />
                        </textarea>
                        </textarea>
                        </textarea></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Awards (include year awarded)</td>
                    <td><textarea  rows="4" name="Awards" />
                        </textarea>
                        </textarea>
                        </textarea></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Professional Organizations/Affiliations</td>
                    <td><input name="Professional Organizations/Affiliations" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Previous Title(s) with Current Organization (include years):</td>
                    <td><input name="Previous Title(s) with Current Organization" type="text" /></td>
                </tr>
            </table>
            <table width="647" border="0" align="center" cellpadding="10" cellspacing="0" class="pageBreak">
                <tr>
                    <td colspan="2"style="font-size:17px;"><br />
                        <strong>Education</strong></td>
                </tr>
                <tr align="left" valign="middle">
                    <td width="40%">Name of School, College or University</td>
                    <td width="60%"><input name="School Name" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Degree</td>
                    <td><input name="Degree" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Year Completed</td>
                    <td><input name="Year Completed" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Name of School, College or University</td>
                    <td><input name="School Name" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Degree</td>
                    <td><input name="Degree" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Year Completed</td>
                    <td><input name="Year Completed" type="text" /></td>
                </tr>
            </table>
            <table width="647" border="0" align="center" cellpadding="10" cellspacing="0">
                <tr>
                    <td colspan="2" style="font-size:17px;"><br />
                        <strong>Veteran Status</strong></td>
                </tr>
                <tr align="left" valign="middle">
                    <td width="40%">Branch</td>
                    <td width="60%"><input name="Branch" type="text" /></td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Years Served</td>
                    <td><input name="Years Served" type="text" /></td>
                </tr>
                <tr>
                    <td colspan="2">&nbsp;</td>
                </tr>
                <tr align="left" valign="middle">
                    <td>Personal</td>
                    <td><textarea  rows="4" name="Personal" />
                        </textarea>
                        </textarea>
                        </textarea></td>
                </tr>
                <td colspan="2" style="text-align: center;"><input name="submit" type="submit" value="Submit Form" /></td>
                </tr>
            </table>
        </form>
    </td>
</tr>

My PHP:

    <?
ob_start();
$now = date('l jS \of F Y h:i:s A');


if (check_data()){
    // Send E-Mail
    echo '<h1 style="margin-top: 20px; text-align:center;">Biography Request Form</h1>';

    send_mail();
    echo '<p style="text-align:center;">Thank you! Your form has been submitted.</p>';
}
else{
    echo '<p style="text-align:center;">Please correct the above errors, and submit your form again. If you are having trouble submitting this form please contact <a href="email@test.com">email@test.com</a></p>';

    exit();
}


function verifyData($str_to_test) {
  $bad_strings = array(
                "content-type:"
                ,"mime-version:"
                ,"multipart/mixed"
                ,"Content-Transfer-Encoding:"
                ,"bcc:"
                ,"cc:"
                ,"to:"
  );

  foreach($bad_strings as $bad_string) {
    if(eregi($bad_string, strtolower($str_to_test))) {
      showerror("$bad_string found. Suspected injection attempt - mail not being sent.");

    }
  }
}

function contains_newlines($str_to_test) {
   if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
     showerror("newline found in $str_to_test. Suspected injection attempt - mail not being sent.");
     exit;
   }
} 

function check_data(){

    // also inspects data for hacking attempts (via verifyData).
    // if verifyData finds something suspicious, the page exists with a warning.
    $isok = true;
    // Check for a person's name.
    if (eregi ("^[[:alnum:].' -]{1,100}", stripslashes(trim($_POST['FirstName'])))) {
    } else {
        $isok = false;
        $errors[] = '<p style="color: #F00; text-align: center;">Please enter your first name!</p>';
    }
    if (eregi ("^[[:alnum:].' -]{1,100}", stripslashes(trim($_POST['LastName'])))) {
    } else {
        $isok = false;
        $errors[] = '<p style="color: #F00; text-align: center;">Please enter your last name!</p>';
    }
    if (eregi ("^[[:alnum:].' -]{1,100}", stripslashes(trim($_POST['Email'])))) {
    } else {
        $isok = false;
        $errors[] = '<p style="color: #F00; text-align: center;">Please enter your email!</p>';
    }
    if (!$isok){
        foreach($errors as $err)
        echo '<div>' . $err . '</div>';
        return false;
    }


    // look for hacker codes.
    verifyData($_POST['Address']);
    verifyData($_POST['City']);
    verifyData($_POST['State']);
    verifyData($_POST['State']);
    verifyData($_POST['Zip']);
    verifyData($_POST['Email']);


    if($_SERVER['REQUEST_METHOD'] != "POST"){
       showerror("Unauthorized attempt to access page.");
    }
    return true;
}

function send_mail(){


    $to = 'email@test.com'; 
    $subject = "Test Mail";

    $txt = "<p>Below is the result of your feedback form.  It was submitted by</p>";
    $txt .= "<table>";
    foreach($_POST AS $field => $value) {
        if (substr($field, 0, 12) == 'Send_info_on')
            $field = 'Box checked:';
        $printIt = true;
        if (substr($field, 0, 18) == 'more_info_on_other'){
                if ($value > ' '){
                    $field = 'Box checked:';
                }else{
                    $printIt = false;
                }
        }

        if ($field == 'submit'){
            $printIt = false;
        }

        if ($printIt){
            $txt .= '<tr><td>' . $field . '&nbsp;</td><td>&nbsp;' . $value . '</td></tr>';
        }
    }
    $txt .= '</table>';
    //echo $txt;

    // Always set content-type when sending HTML email
    $headers  = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= "From: webmaster@test.com" . "\r\n";
    $headers .= "CC: email@test.com". "\r\n";

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


}

function showerror($msg) { 
  ?>
<html>
<head>
<title>Sorry, Please Try Again</title>
</head>
<body bgcolor=#FFFFFF text=#000000>
<center>
    <table border=0 width=600 bgcolor=#9C9C9C>
        <tr>
            <th><font size=+2>Oops!</font></th>
        </tr>
    </table>
    <table border=0 width=600 bgcolor=#CFCFCF>
        <tr>
            <td>Something you entered did not pass our security checks.</td>
        <tr>
            <td><? echo $msg ?></td>
        </tr>
        <tr>
            <td>Please check your data, then submit again.</td>
        </tr>
    </table>
</center>
</body>
</html>
<?
ob_end_flush();
exit();

}
ob_end_flush();
?>

How many you are closing??

   <textarea rows="4" name="Role/Responsibilities" />
                    </textarea>
                    </textarea>
                    </textarea>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM