How to Code phpMailer to prevent sending email if

2019-09-19 08:20发布

问题:

I like phpMailer but when I used a previous mailer it had an anti spam code.


You added a hidden field in the contact form and the mail.php script was coded that if the hidden field was filled in (i.e. only a spam robot would do that) the mail wouldnt send


How would I add that to this script?

This is my mail.php code as follows


<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$address = $_REQUEST['address'] ;
$postcode = $_REQUEST['postcode'] ;
$service = $_REQUEST['service'] ;
$height = $_REQUEST['height'] ;
$how = $_REQUEST['how'] ;
$website = $_REQUEST['website'] ;
$mail_intro ="The following enquiry came from your web site:";
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("class.phpmailer.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "franchise@website.co.uk";  // SMTP username
$mail->Password = "passowrd"; // SMTP password
$mail->From = "noreply@website.co.uk";
$mail->FromName = "B";
$mail->AddReplyTo($email,$name);
$mail->AddAddress("franchise@website.co.uk", "B");
$mail->AddBCC("joseph@website.co.uk", "Joseph");
$response="$mail_intro\n<br />Name: $name\n<br />Email: $email\n<br />Phone: $phone\n<br />Address: $address\n<br /> Comments:\n$message\n<br /> Website: $website\n";

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "Enquiry";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $response;
$mail->AltBody = $message;
//Autoresponder
$mailto="$email";
$subject="Franchisee Enquiry";

$body.="Thanks for your enquiry, the message below has been sent. \n";
$body.="If due to an unknown technical error you do not receive a response within 4 hours please phone  \n";
$body.="or email us directly at info@website.co.uk where we will be only too happy to help.\n";
$body.="$mail_intro\n";
$body.="Name: $name\n";
$body.="Email: $email\n";
$body.="Phone: $phone\n";
$body.="Postcode: $postcode\n";
$body.="Service Required: $service\n";
$body.="How found: $how\n";
$body.="Comments:\n$message\n";
mail($mailto,$subject,$body,"From: noreply@website.co.uk\n");
if(!$mail->Send())
{
   header("Location: http://website.co.uk/error.php");
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

header("Location: http://website.co.uk/thankyou.php");

?>

回答1:

In your HTML, add:

<input type="text" name="fooBarBaz" style="display: none">

And in your PHP:

if ( !empty($_REQUEST['fooBarBaz']) )
{
    // hidden field was filled out, do something about it
}
else
{
    $email = $_REQUEST['email'];
    ...
}