当前防止 PHP 中电子邮件注入攻击的最佳实践是什么?
如今,清理 PHP 电子邮件表单中的数据的最佳实践是什么?
我目前正在使用类似的东西...
$msg = $_POST['msg'];
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$name = $_POST['name'];
$subject = "Message from the MY_WEBSITE website e-mail system";
$message = "From: " . $name . "\n";
$message .= "Email: " . $email . "\n\n";
$message .= $msg;
$headers = "From: " . $email . "\r\n" .
"Reply-To: " . $email . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$mailSuccess = mail("[email protected]", $subject, $message, $headers);
以这种方式简单地过滤电子邮件字段是否足以提供足够的保护? 我可以/应该更有效地强化脚本以防止垃圾邮件发送者吗?
提前致谢!
[编辑]澄清,因为到目前为止的答案表明我没有很好地解释自己。
我主要关心的不是垃圾邮件机器人获取此脚本,而是任何人利用它向 [电子邮件受保护]
。 这可能包括机器人,但同样可能是人类通过验证码测试。
我正在寻找的是 PHP,它将确保 mail() 方法发送的电子邮件不被劫持。 这可能是一个正则表达式或过滤器或类似的东西,只是简单地删除某些字符。 再次感谢。[/编辑]
What's considered the best practice these days for sanitizing data from a PHP email form?
I'm currently using something like this...
$msg = $_POST['msg'];
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$name = $_POST['name'];
$subject = "Message from the MY_WEBSITE website e-mail system";
$message = "From: " . $name . "\n";
$message .= "Email: " . $email . "\n\n";
$message .= $msg;
$headers = "From: " . $email . "\r\n" .
"Reply-To: " . $email . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$mailSuccess = mail("[email protected]", $subject, $message, $headers);
Is it sufficient protection to simply filter the email field in this fashion? Can/should I harden the script more effectively to protect against spammers?
Thanks in advance!
[EDIT]Clarification, since the answers so far suggest that I've not explained myself well.
I'm not principally concerned with spambots getting hold of this script, but with anyone utilizing it to send illicit emails to any address other than [email protected]
. This might include a bot, but could equally be a human defeating a CAPTCHA test.
What I'm looking for is PHP that will ensure that the email sent by the mail() method is not hijacked. This is probably a regex or filter or similar that simply strips certain characters. Thanks again.[/EDIT]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会这样做:
I would do this: