安装reCaptcha后,PHPMailer发送垃圾邮件

时间:2018-02-09 17:14:43

标签: php phpmailer recaptcha spam

我使用PHP Mailer并启用了Google的reCaptcha。看起来像垃圾邮件的电子邮件仍然可以通过。我还能做些什么来帮助消除看似垃圾邮件的电子邮件吗?

以下是我的send_email.php文件的代码

<?php

$errors =  array('');
$missing = array('');
$expected = array('');


if (isset($_POST['submit'])) {
    $required = array('Name','Phone','Email');
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];

   $mailSent = false;
// Assume the input contains nothing suspect
$suspect = false;
// Regular expression to search for suspect phrases
$pattern = '/Content-type:|Bcc:|Cc:|<a|url=|http|â|€|/i';

// Recursive function that checks for suspect phrases
// Third argument is passed by reference
function isSuspect($value, $pattern, &$suspect) {
    if (is_array($value)) {
        foreach ($value as $item) {
            isSuspect($item, $pattern, $suspect);
        }
    } else {
        if (preg_match($pattern, $value)) {
            $suspect = true;
        }
    }
}

// Check the $_POST array for suspect phrases
isSuspect($_POST, $pattern, $suspect);
 $messagef .='The Following was submitted:<br /><br />';
 $messagef .='<table border="0" cellspacing="0" >';
        foreach ($_POST as $key => $value) {

            if($key !='submit' && $key!='g-recaptcha-response') {
                    $messagef .='<tr style=\'border-bottom:1px solid #ccc; padding:5px\'>';
                    $messagef .='<td style=\'border-bottom:1px solid #ccc; padding:5px 10px 3px 2px\'>'.$key.':   </td>';
                    $messagef .='<td style=\'border-bottom:1px solid #ccc; padding:5px 10px 3px 10px\'>'.strip_tags($value).'</td>';
                    $messagef .='</tr>';
            }
        }
// Add IP Field
 $ip = $_SERVER['HTTP_CLIENT_IP']?$_SERVER['HTTP_CLIENT_IP']:($_SERVER['HTTP_X_FORWARDE‌​D_FOR']?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR']);
                    $messagef .='<tr style=\'border-bottom:1px solid #ccc; padding:5px\'>';
                    $messagef .='<td style=\'border-bottom:1px solid #ccc; padding:5px 10px 3px 2px\'>User IP:   </td>';
                    $messagef .='<td style=\'border-bottom:1px solid #ccc; padding:5px 10px 3px 10px\'>' .$ip. '</td>';
                    $messagef .='</tr>';
// End IP Field     
        $messagef .= '</table>';
// ReCaptcha Addition Start        
if(!isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']) ){
 echo "Please click on the reCAPTCHA box";
 exit;
 }
         $secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if(!$responseData->success) {
             echo "Robot verification failed, please try again";
             exit;
        }
// ReCaptcha Addition End
// Process the form only if no suspect phrases are found
if (!$suspect) :
    // Check that required fields have been filled in,
    // and reassign expected elements to simple variables
        $messagefull = $messagef;
        $cc = '';
        $to = 'email@address.com';
        $subject = 'Website Contact Form'.' - '.$Name;
         require 'PHPMailer/PHPMailerAutoload.php';

            //Create a new PHPMailer instance
            $mail = new PHPMailer;

            //Set who the message is to be sent from
            $mail->setFrom('info@address.com' ,'Form Submission');
            //Set an alternative reply-to address
            $mail->addReplyTo($Email, $Name);
            //Set who the message is to be sent to
            $mail->addAddress($to, '');

            //add cc email
        if(!empty($cc)) {
            $mail->addCC($cc);
        }
        if(!empty($bcc)) {
            //add bcc email
            $mail->addBCC($bcc);
        }
            //Set the subject line
            $mail->Subject = strip_tags($subject);
            //Read an HTML message body from an external file, convert referenced images to embedded,
            //convert HTML into a basic plain-text alternative body
            $mail->msgHTML($messagefull);
            //Replace the plain text body with one created manually
            $mail->AltBody = 'This is a plain-text message body';
            //Attach an image file
            //$mail->addAttachment('images/phpmailer_mini.png');

            //send the message, check for errors
            if (!$mail->send()) {
                $error .= $mail->ErrorInfo; 
            } else {
                $error = 1;
            }

        if ($error==1) {
       echo 'Email has been sent';
         } else {
        echo "Couldn't send email";
}
    endif;

  /*   if ($mailSent) {
        echo 1; 
    }else{
        echo 0;
    } */
     exit;
}
?>

我还使用form-validate.js验证要填写的字段:

$("#Contact").validate({
             errorElement : 'div',
             errorLabelContainer: '.errorTxt',
            rules: {
                Name: "required",
                Phone: "required", 
                Email: {
                    required: true,
                    email: true
                } 
            },
            messages: {
                Name: "Please enter your first name",
                Phone: "Please enter your phone number", 
                Email: "Please enter a valid email address", 
            },
            submitHandler:function(){

                $.ajax({
                       type: "POST",
                       url: "form/send_email",
                       data: $("#Contact").serialize(), // serializes the form's elements.
                       success: function(data)
                       {  
                           if($.trim(data)=="Email has been sent"){
                            window.location="thankyou.php";                        
                           }else{
                             alert(data);
                            return false;   
                           }
                       },
                       error:function(){
                            alert("Sorry, Internal server error, Please try again later!");
                            return false;
                       }
                     });
                return false;
                e.preventDefault(); // avoid to execute the actual submit of the form.

                        }
    });

0 个答案:

没有答案