Php if语句/联系人形成反垃圾邮件集成

时间:2014-08-25 22:55:33

标签: php email sendmail

我正在尝试将this tutorial整合到我现有的联系表单中。但是,如果它不起作用。基本上我想要一个空白的url输入字段,我用CSS隐藏。如果该字段填写在表单中将不会发送。这是我原来的邮件脚本:

<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "rrswans@gmail.com";

    // Set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        //http_response_code(200);
        echo "Thank You! Message Received! Click to close. ";
    } else {
        // Set a 500 (internal server error) response code.
        //http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
   // http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

&GT;

这是我需要集成的代码......

<?php 
// if the url field is empty 
if(isset($_POST['url']) && $_POST['url'] == ''){
     // then send the form to your email
      mail( 'you@yoursite.com', 'Contact Form', print_r($_POST,true) ); 
} 
// otherwise, let the spammer think that they got their message through
?>

我确信这很容易,我只是不太了解PHP。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:2)

尝试更改:

    if (mail($recipient, $subject, $email_content, $email_headers)) {...

致:

    if (isset($_POST['url']) && $_POST['url'] == '' &&
       mail($recipient, $subject, $email_content, $email_headers)) {...

此外,您的隐藏输入被称为“蜜罐”,如果设置为:

,它会更有效。
position:absolute;
top: -100;

而不是

display:none

修改

我忘了if语句从左到右验证,请参阅上面的修订版。 还要确保您的输入名为“url”

<input name="url" />