出于某种原因,当我将我的邮件代码放在if语句中时,它不再有效。它正在检测是否在表单字段中输入了单词hot。 else语句工作正常,但输入正确的单词时没有任何反应。我无法弄清楚原因。
$botdetect = $_POST['botdetect'];
if ($botdetect == 'hot') {
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '-----'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '-----'; // SMTP username
$mail->Password = '------'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('------', '------');
$mail->AddAddress($_POST['recipient']); // Add a recipient
//$mail->addCC('cc@example.com'); //Add if needed
//$mail->addBCC('bcc@example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->Subject = 'You Have A Message!'; //Subject Line
// Retrieve the email template required
$message = file_get_contents('template/template.html');
// Replace the % with the actual information
$message = str_replace('%name%', $_POST['name'], $message);
$message = str_replace('%email%', $_POST['email'], $message);
$message = str_replace('%phone%', $_POST['phone'], $message);
$message = str_replace('%message%', $_POST['message'], $message);
//Set the message
$mail->MsgHTML($message);
$mail->send();
echo "<script type='text/javascript'>alert('Message Sent!');</script>";
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
else {
echo "<script type='text/javascript'>alert('Bot Detected!');</script>";
}
以下是表单的代码:
<body>
<!-- iframe and form method, action, and target must be the same as shown here-->
<iframe name="sugar-mail" style="display: none;"></iframe>
<form method="post" action="mail.php" target="sugar-mail">
<!--input with id recipient is needed to send the email. Obfuscated address to prevent spam.-->
<script>var x=document.createElement("INPUT");x.setAttribute("id","recipient"),x.setAttribute("name","recipient"),x.setAttribute("type","hidden"),x.setAttribute("value","austin@sugar-rock.com"),document.body.appendChild(x);</script>
Email: <input name="email" id="email" type="text" required /><br /> Message:
<br />
<textarea name="message" id="message" rows="5" cols="40" required></textarea><br />
<!--Bot Detection -->
Is fire hot or cold? <input name="botdetect" id="botdetect" type="text" required />
<br />
<input type="submit" value="Submit" />
</form>
</body>