-3
    PHP

<?php
$field_name = $_POST['cf_name'];
$field_subject = $_POST['cf_subject'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'wermedia@hotmail.com';
$subject = 'Melding fra nettsiden: '.$field_subject;
$mail_noreply = 'noreply@wermedia.com';

$body_message = 'From: '.$field_name."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = "From: $field_name <$field_email> \r\n";
$headers .= "Reply-To: .$field_email.\r\n";
$headers .= "Return-Path: .$field_email.\r\n";
$headers .= "X-Mailer: Drupal \n";
$headers .= 'MIME-Version: 1.0' . "\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

$confirmationSubject='Confirmation message';
$displayForm=true;

if ($_POST){
$field_email=stripslashes($_POST['cf_email']);
$field_message=stripslashes($_POST['cf_message']);
// validate e-mail address
$valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$field_email);
$crack=eregi("(\r|\n)(to:|from:|cc:|bcc:)",$field_message);
if ($field_email && $field_message && $valid && !$crack){
if (mail($mail_to,$confirmationSubject,$bodymessage.$field_message,'From: '.$mail_noreply."\r\n")){
$displayForm=false;
?>
    <script language="javascript" type="text/javascript">
        alert('Your message was successfully sent. In addition, a confirmation copy was sent to your e-mail address.');
        window.location = 'contact.htm';
    </script>
<?php
        echo '<script>'.htmlspecialchars($field_message).'</script>';
      }else{ // the messages could not be sent
?>
    <script language="javascript" type="text/javascript">
        alert('Something went wrong when the server tried to send your message. This is usually due to a server error, and is probably not your fault. We apologise for any inconvenience caused. Click OK and go one step back, to see your message.');
        window.location = 'contact.htm';
    </script>
<?php
      }
    }else if ($crack){ // cracking attempt
?>
    <script language="javascript" type="text/javascript">
        alert('Your message contained e-mail headers within the message body. This seems to be a cracking attempt and the message has not been sent. Click OK and go one step back, to see your message.');
        window.location = 'contact.htm';
    </script>
<?php
    }else{ // form not complete
?>
    <script language="javascript" type="text/javascript">
        alert('Your message could not be sent. You must include both a valid e-mail address and a message. Click OK and go one step back, to see your message.');
        window.location = 'contact.htm';
    </script>
<?php
    }
  }
?>

HTML

<form action="contact.php" method="post">
  Name<br>
  <input id="phpinput" type="text" name="cf_name"><br>
  Subject<br>
  <input id="phpinput" type="text" name="cf_subject"><br>
  E-mail<span style="color: red">*</span><br>
  <input id="phpinput" type="text" name="cf_email"><br>
  Message<span style="color: red">*</span><br>
  <textarea name="cf_message"></textarea><br>
  <input class="mob_sendclear" style="font-family: bankgothic;" type="submit" value="Send">
  <input class="mob_sendclear" style="font-family: bankgothic;" type="reset" value="Clear">
</form>

我觉得这一切都很好,除了当你收到邮件不正常的消息时,我仍然收到邮件......所以如果你发送邮件并收到错误消息,我仍然会在收件箱中收到邮件。

谁能告诉我该怎么做,在哪里?

我还想放一个简单但非常安全的反垃圾邮件代码。

4

1 回答 1

-1

只需验证客户端

jquery-validate 或使用带有内置验证的主干表单,

通常这是一个前端而不是 php 问题,并且邮件的服务器端验证有点矫枉过正,因为对于每一个错误的提交,你都会响起你的服务器端,这是业务 2 没有人,就个人而言,拥有大部分东西对用户性能更好在客户端,只是我的意见

于 2013-02-09T12:04:52.910 回答