【发布时间】:2011-02-24 14:47:44
【问题描述】:
有没有更好的方法来阻止垃圾邮件通过我的 phpmailer 脚本?
另外,我将如何为此添加格式,以便在通过电子邮件发送时更具可读性,例如断线
我希望我的 php 语法是正确的 - 因为我不懂 PHP。
<?php
# bool has_injection (String $var, [String $var, ...])
function has_injection () {
$values = func_get_args();
for ($i=0, $count=func_num_args(); $i<$count; $i++) {
if ( stristr($values[$i], "%0A") || stristr($values[$i], "%0D") || stristr($values[$i], "\\r") || stristr($values[$i], "\\n")
|| stristr($values[$i], "Bcc") || stristr($values[$i], "Content-Type") ) {
return true;
}
}
return false;
}
$error = '';
if (isset($_POST) && count($_POST)>0) {
# The form has been submitted
$course_title = $_POST['course_title'];
$course_date = $_POST['course_date'];
$course_code = $_POST['course_code'];
$course_fee = $_POST['course_fee'];
$break .= "\n";
$qual_subject_level = $_POST['qual_subject_level'];
$break .= "\n";
$email = $_POST['email'];
if ($name && $email && $subject) {
if (has_injection($name, $email, $subject)) {
# You've got another spammer at work here
$error = 'No spamming';
exit(0);
}
else {
# It's safe to send the message
mail('my@gmail.com',
$subject,
$course_title,
$course_code,
$course_fee,
$break,
$qual_subject_level,
$break,
$subject,
"From: $name <$email>");
}
}
else {
$error = 'Please fill in all the forms';
}
}
?>
【问题讨论】:
-
就像一个注释, if (isset($_POST) && count($_POST)>0) { 可以通过页面上的一个按钮将数据发送给您自己最好检查每个字段.您可以使用正则表达式来确保使用有效的电子邮件
标签: php forms formatting spam-prevention phpmailer