Ale*_*exV 7 php security swiftmailer contact-form email-injection
我的网站上有一个PHP驱动的HTML联系表单.目前我使用PHP mail()函数.因此,我必须做许多用户输入验证,以避免电子邮件头注入攻击.我认为我很安全,但我可能忘了一些东西,我想转移到一个可靠的PHP电子邮件库.我选择的库是Swiftmailer.
现在我想检查Swiftmailer是否解决了以下问题:
<,并>在发送者名称的字符.\n,\r\n...).\n应该在内容和\r\n电子邮件标题分隔符中使用.PS:我试图联系Swiftmailer团队,我的问题没有成功,所以我在这里尝试.
编辑:
我用Swiftmailer做了一些测试用例,这是我到目前为止所发现的:
<或>以发件人的名义时,您将收到无法发送的电子邮件错误邮件.这可能会导致你的邮件服务器的DOS攻击(也许我错了).这是正常的吗?!有人可以为我澄清#1和#4吗?我不确定这是否正常......
编辑:这个答案可能已过时。在我写这篇文章时,SwiftMailer 库存在一些问题。此时,一切都工作正常,并且SwiftMailer被认为是更好的库,比 提供更多功能PHPMailer。
我建议你使用 phpmailer。它是我用过的最稳定的邮件库之一。这是一个应该可以工作的示例代码:
include("./phpmailer/class.phpmailer.php");
$mail = new PHPMailer(false); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP();
$mail->Host = "YourDomainName.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "YourSMTPMailServer.com";
$mail->Port = 587;
$mail->Username = "your-auth-user@yoursmtpmailsercer.com";
$mail->Password = "password"; // GMAIL password
$mail->AddAddress("sendToThis@email.com", '<< >> ! " Receiver Name');
$mail->SetFrom('sendFROMthis@email.com', '<< >> ! " Sender Name');
$mail->Subject = "A testing subject";
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('This is my <b>html</b> testing email, sent '.time());
$mail->Send();
Run Code Online (Sandbox Code Playgroud)
您需要对其进行配置,以便它连接到您的电子邮件服务器,但它应该可以正常工作。Phpmailer 逃脱了到目前为止我尝试过的一切。我唯一检查的是“sendToThis@email.com”。我用这段代码来做:
$email = "sendToThis@email.com";
$email = filter_var(filter_var($email,FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL);
if($email){
echo "This email is valid!";
} else {
echo "This email is INVALID!";
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助 :)