7

我的网站上有一个 PHP 驱动的 HTML 联系表。目前我使用 PHP mail()函数。因此,我必须进行许多用户输入验证以避免电子邮件标头注入攻击。我认为我很安全,但我可能忘记了一些事情,我想转移到一个可靠的 PHP 电子邮件库。我选择的库是Swiftmailer

现在我想检查 Swiftmailer 是否解决了以下问题:

  1. 删除或转义发件人姓名中<>字符。
  2. 从发件人姓名中删除换行符 ( \n, \r\n...)。
  3. 从电子邮件主题中删除或转义换行符。
  4. 规范化消息正文(电子邮件的内容)中的换行符。根据 PHP 文档,\n应在内容中使用并\r\n作为电子邮件标题分隔符。

PS:我试图联系 Swiftmailer 团队并提出我的问题,但没有成功,所以我在这里尝试。

编辑:

我用 Swiftmailer 做了一些测试用例,这是我目前发现的:

  1. 当您有一个<>以发件人的名义时,您会收到一封无法投递的电子邮件错误邮件。这可能会导致您的邮件服务器受到DOS 攻击(也许我错了)。这正常吗?!
  2. 换行符被转义,因此注入攻击失败。
  3. 换行符被转义,因此注入攻击失败。
  4. 经过测试,但我无法看到 Swiftmailer 做了什么(如果它做了什么)。所以我在这里仍然处于黑暗之中。

有人可以为我澄清#1 和#4 吗?我不确定这是否是正常行为...

4

1 回答 1

1

编辑:这个答案可能已经过时了。在我写这篇文章的时候,Sw​​iftMailer 库存在一些问题。在这一点上,一切都可以正常工作,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();

您需要对其进行配置,以便它连接到您的电子邮件服务器,但它应该可以工作。到目前为止,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!";
}

我希望这有帮助 :)

于 2011-07-07T16:07:02.590 回答