[英]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.