0

作为 PHP 语言的新手,我设置了一个带有 html 编码以及 Javascript 验证代码的 php 联系表单 (contact.php)。完成后,所有的事情都没有提交并到达我给定的电子邮件 ID。真的,我无法弄清楚发生问题的代码。请您检查以下这些代码并帮助我解决这个问题吗?

<?php

// Set email variables
$email_to = 'contact@mycompany.com';
$email_subject = 'New Contact Form Submission';

/* Gathering Data Variables */

    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $country = $_POST['country'];
    $message = $_POST['message'];
    $ipaddress = $_POST['ipaddress'];

    $body = <<<EOD
<br><hr><br>
Full Name: $fullname <br>
Email: $email <br>
Phone: $phone <br>
Country: $country <br>
Message: $message <br>
Submitted By IP: $ipaddress <br>
EOD;

    $headers = "From: $fullname\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body,
$headers);

// Set required fields
$required_fields = array('fullname','email','country','message');

// set error messages
$error_messages = array(
    'fullname' => 'Please enter Your Name to proceed.',
    'email' => 'Please enter a valid Email Address to contact.',
    'country' => 'Please enter your country of residence.',
    'message' => 'Please enter your message.'
);

// Set form status
$form_complete = FALSE;

// configure validation array
$validation = array();

// check form submittal
if(!empty($_POST)) {
    // Sanitise POST array
    foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

    // Loop into required fields and make sure they match our needs
    foreach($required_fields as $field) {       
        // the field has been submitted?
        if(!array_key_exists($field, $_POST)) array_push($validation, $field);

        // check there is information in the field?
        if($_POST[$field] == '') array_push($validation, $field);

        // validate the email address supplied
        if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
    }

    // basic validation result
    if(count($validation) == 0) {
        // Prepare our content string
        $email_content = 'Contact Form Submission Received: ' . "\n\n";

        // simple email content
        foreach($_POST as $key => $value) {
            if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
        }

        // if validation passed ok then send the email
        mail($email_to, $email_subject, $email_content);

        // Update form switch
        $form_complete = TRUE;
    }
}

function validate_email_address($email = FALSE) {
    return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}

function remove_email_injection($field = FALSE) {
   return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}

?>

表格开始

<?php if($form_complete === FALSE): ?>
                                        <form name="contact_form" method="post" id="contact_form" action="contacts.php" onsubmit="return defaultagree(this)"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Name:</td>
    <td valign="top" align="left" width="300" class="mbox"><input name="fullname" id="fullname" type="text" class="offer_input_box" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" /><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?></td>
  </tr>
</table>
</td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Email:</td>
    <td valign="top" align="left" width="300" class="mbox"><input name="email" id="email" type="text" class="offer_input_box" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" /><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?></td>
  </tr>
</table>
    </td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext">Phone:</td>
    <td valign="top" align="left" width="300"><input name="phone" id="phone" type="text" value="" class="offer_input_box" /></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Country:</td>
    <td valign="top" align="left" width="300" class="mbox"><input name="country" id="country" type="text" class="offer_input_box" value="<?php echo isset($_POST['country'])? $_POST['country'] : ''; ?>" /></textarea><?php if(in_array('country', $validation)): ?><span class="error"><?php echo $error_messages['country']; ?></span><?php endif; ?></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="right" width="130" class="offerformtext"><span>*</span> Message:</td>
    <td valign="top" align="left" width="300" class="mmbox"><textarea name="message" id="message" class="offer_message_box" value="<?php echo isset($_POST['message'])? $_POST['message'] : ''; ?>" /></textarea><?php if(in_array('message', $validation)): ?><span class="error"><?php echo $error_messages['message']; ?></span><?php endif; ?></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td valign="bottom" align="center" class="subdown"><input name="submit" type="Submit" class="offer_submit_button" value=""/><br /><br /></td>
  </tr>
  <tr>
    <td><input type="hidden" name="ipaddress" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" /></td>
  </tr>
</table>
</form>
<?php else: ?>
                                                <h2 style="font-family:'Times New Roman', Times, serif; font-size:24px; color:#6d6d6d; font-weight:bold;">Contact Form Successfully Submitted</h2>
<p style="font-family:'Times New Roman', Times, serif; font-size:18px; color:#255E67; margin-left:25px; margin-top:15px;">Thank you for your get in touch with us! We would get back to you regarding this information soon.</p>
<script type="text/javascript">
setTimeout('ourRedirect()', 8000)
function ourRedirect(){
    location.href='contacts.php'
}
</script>
<?php endif; ?>
                                    <script>
document.forms.contact_form.agreecheck.checked=false
</script>

表格结束

这些是我使用过的代码,请帮助我解决这个问题。先感谢您。

4

1 回答 1

0

我稍微改变了你的代码。您似乎在验证电子邮件之前正在构建它。您可以将所有代码放在一个文件中,而不是两个。

<?php

  // Set email variables
$email_to = 'contact@mycompany.com';
$email_subject = 'New Contact Form Submission';
$ipaddress=$_SERVER['REMOTE_ADDR'];

// Set required fields
$required_fields = array('fullname','email','country','message');

// set error messages
$error_messages = array(
    'fullname' => 'Please enter Your Name to proceed.',
    'email' => 'Please enter a valid Email Address to contact.',
    'country' => 'Please enter your country of residence.',
    'message' => 'Please enter your message.'
);



// check form submittal
if(!empty($_POST)) {


// Sanitise POST array
foreach($_POST as $key=>$value){ 

if($key=="fullname"){$fullname=str_replace( '[at]','@', $value);}
if($key=="country"){$country=str_replace( '[at]','@', $value);}
if($key=="message"){$message=str_replace( '[at]','@', $value);}
if($key=="phone"){$phone=str_replace( '[at]','@', $value);}

if($key=="email"){
if(!filter_var($value, FILTER_VALIDATE_EMAIL)) { $error_out[$key]=$error_messages[$key];} else{$email=$value;}
   }

// check required fields
        if(in_array($key, $required_fields) and empty($value))
        {
             $error_out[$key]=$error_messages[$key]; // errors for required
        }

if($key=="amigura"){$amigura='';}else{$amigura='';}

}

$body = <<<EOD
<br><hr><br>
Full Name: $fullname <br>
Email: $email <br>
Phone: $phone <br>
Country: $country <br>
Message: $message <br>
Submitted By IP: $ipaddress <br>
EOD;

//echo $body;   

    // send mail
    if(!$error_out){

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "From: $fullname <$email> \r\n";
$headers .= "Content-type: text/html;charset=UTF-8 \r\n";

    if(mail($email_to, $emailSubject, $body,$headers)){$form_complete='sendme';}

        }
        else
        {$form_complete='';}

}


?>

<?php if($form_complete != 'sendme'): ?>
                                        <form name="contact_form" method="post" id="contact_form" action="" onsubmit="return defaultagree(this)"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Name:</td>
    <td valign="top" align="left" width="300" class="mbox"><input name="fullname" id="fullname" type="text" class="offer_input_box" value="<?php echo htmlspecialchars($_POST['fullname'], ENT_QUOTES); ?>" /><?php if($error_out['fullname']): ?><span class="error"><?php echo $error_out['fullname']; ?></span><?php endif; ?></td>
  </tr>
</table>
</td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Email:</td>
    <td valign="top" align="left" width="300" class="mbox"><input name="email" id="email" type="text" class="offer_input_box" value="<?php echo htmlspecialchars( $_POST['email'] , ENT_QUOTES); ?>" /><?php if($error_out['email']): ?><span class="error"><?php echo $error_out['email']; ?></span><?php endif; ?></td>
  </tr>
</table>
    </td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext">Phone:</td>
    <td valign="top" align="left" width="300"><input name="phone" id="phone" type="text" value="<?php echo htmlspecialchars( $_POST['phone'] , ENT_QUOTES); ?>" class="offer_input_box" /></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Country:</td>
    <td valign="top" align="left" width="300" class="mbox"><input name="country" id="country" type="text" class="offer_input_box" value="<?php echo htmlspecialchars( $_POST['country'], ENT_QUOTES); ?>" /></textarea><?php if($error_out['country']): ?><span class="error"><?php echo $error_out['country']; ?></span><?php endif; ?></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="right" width="130" class="offerformtext"><span>*</span> Message:</td>
    <td valign="top" align="left" width="300" class="mmbox"><textarea name="message" id="message" class="offer_message_box" value="<?php echo htmlspecialchars($_POST['message'], ENT_QUOTES); ?>" /></textarea><?php if($error_out['message']): ?><span class="error"><?php echo $error_out['message']; ?></span><?php endif; ?></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td valign="bottom" align="center" class="subdown"><input name="submit" type="Submit" class="offer_submit_button" value=""/><br /><br /></td>
  </tr>
</table>
</form>
<?php else: ?>
                                                <h2 style="font-family:'Times New Roman', Times, serif; font-size:24px; color:#6d6d6d; font-weight:bold;">Contact Form Successfully Submitted</h2>
<p style="font-family:'Times New Roman', Times, serif; font-size:18px; color:#255E67; margin-left:25px; margin-top:15px;">Thank you for your get in touch with us! We would get back to you regarding this information soon.</p>
<script type="text/javascript">
setTimeout('ourRedirect()', 8000)
function ourRedirect(){
    location.href='contacts.php'
}
</script>
<?php endif; ?>

<script>
//document.forms.contact_form.agreecheck.checked=false
</script>
于 2013-06-14T15:44:38.643 回答