[英]Receiving Spam from my Form Using PHPMailer
我之所以要使用stackoverflow,是因为我搜索的所有内容几乎都涉及使用PHPMailer将表单发送给用户的垃圾邮件箱中的电子邮件。 但是,我需要有关从表单本身接收垃圾邮件的信息。 我在交通流量很小的小型房地产代理商网站上使用它。 她会不时收到垃圾邮件,但我不知道该如何解决。 PHPMailer似乎是使用PHP发送电子邮件的首选工具,因此我认为垃圾邮件/安全性已被很好地涵盖了。 我一定做错了...。当然,我在使用class.phpmailer.php,这是我的代码:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$phone = trim($_POST["phone"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $phone == "" OR $message == "") {
echo "You must specify a value for name, email address, phone, and message.";
exit;
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
echo "There was a problem with the information you entered.";
exit;
}
}
if ($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
require_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
exit;
}
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Phone: " . $phone . "<br>";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom($email, $name);
$address = "email@domain.com";
$mail->AddAddress($address, "A Name Here");
$mail->Subject = "Message from " . $name . " on website contact form";
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
header("Location: index.php?status=thanks");
exit;
}
HTML非常简单:
<form id="form" name="form" method="post" action="contact-process.php">
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p class="form-thanks">Thank you for contacting us. We'll be in touch with you very soon.</p>
<?php } ?>
<label>Name
<span class="small">First and Last</span>
</label>
<input type="text" name="name" id="name" />
<label>E-Mail
<span class="small">name@email.com</span>
</label>
<input type="text" name="email" id="email" />
<label>Phone Number
<span class="small">With area code</span>
</label>
<input type="text" name="phone" id="phone" />
<label>Message
<span class="small">How can we help you?</span>
</label>
<textarea cols="40" rows="8" name="message"></textarea>
<button type="submit">Submit</button>
<div class="spacer"></div>
</form>
避免垃圾邮件的一种简单方法是使用一种称为“蜜罐”的东西,这是一个普通用户不可见的文本字段,但一个垃圾邮件机器人可能会在该字段中输入一些内容。
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// robot detection
$honeypot = trim($_POST["email"]);
if(!empty($honeypot)) {
echo "BAD ROBOT!";
exit;
}
$name = trim($_POST["name"]);
$email = trim($_POST["real_email"]);
$phone = trim($_POST["phone"]);
$message = trim($_POST["message"]);
// rest stays as is
在HTML文件中,您需要插入另一个“蜜罐”(honeypot)“文本”字段:
<label>E-Mail
<span class="small">name@email.com</span>
</label>
<input type="text" name="email" style="display: none;">
<input type="text" name="email_real" id="email" />
注意如何将实际可见的电子邮件文本字段的名称更改为“ email_real”。 最好在真实的电子邮件字段中完全避免使用“电子邮件”一词,因为许多机器人很笨。
但是,不可见的蜜罐输入字段应称为“电子邮件”。 为什么? 由于大多数机器人都在扫描某些标准输入字段,例如“电子邮件”,“地址”等-因此,给蜜罐一个通用的表单字段名称很重要。
另一个巧妙的技巧是交换一些常用的字段名称,即交换电子邮件和邮政编码字段的名称,因此机器人将为电子邮件地址填写数字值,并为邮政编码填写电子邮件地址,这将使验证失败。
这不是100%保证消除所有垃圾邮件,但对我而言效果很好,而不会强迫用户解决烦人的验证码...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.