【问题标题】:PHPmailer internal functions for validation/sanitazion vs external用于验证/清理的 PHPmailer 内部函数与外部函数
【发布时间】:2013-04-08 22:31:13
【问题描述】:

在寻找了大约一周的好答案后,我意识到我必须寻求帮助。一个星期以来,我一直在纠结是否应该使用 PHP-mailer 或 Swiftmailer,我决定使用 PHP-mailer,因为我觉得文档更全面,而且还有更多示例。查看代码后,我发现 PHP-mailer 有许多内置的卫生/验证功能。我想知道是否有人发现它们有任何缺点或漏洞?你会如何使用它们?为了让问题更具体一点,我想知道您将如何使用 phpmailer 验证/清理 process.php 文件中的以下表单数据。

<form class="form-horizontal" id="contact-form-info" action="func/mail/mail-info.php"    method="post">
<fieldset>

<div class="control-group">
    <label class="control-label" for="name">Namn</label>
    <div class="controls">  
    <input type="text" class="input-xlarge" name="name" id="name" maxlength="50" placeholder="Namn *"/>
    </div>
</div>

<div class="control-group">
  <label class="control-label" for="foretag">Företag</label>
  <div class="controls">  
    <input type="text" class="input-xlarge" name="foretag" id="foretag" maxlength="50" placeholder="Företag" />
    </div>
</div>

<div class="control-group">
    <label class="control-label" for="email">E-post</label>
    <div class="controls">
    <input type="text" class="input-xlarge" name="email" id="email" placeholder="E-post *"/>
   </div>
</div> 
<div class="control-group">
    <label class="control-label" for="subject">Ämne</label>
    <div class="controls">
    <input type="text" class="input-xlarge" name="subject" id="subject" maxlength="5000"   placeholder="Ämne *"/>
    </div>
</div>   
<div class="control-group">
<label class="control-label" for="message">Meddelande</label>
<div class="controls">
    <textarea class="input-xlarge" name="message" placeholder="Meddelande *"  id="message" rows="3"></textarea>
  </div>
  </div>

    <div class="form-actions">
  <input id="submit" name="submit" type="submit" value="Skicka" class="btn btn-custom btn-large"></input>
<input type="reset" class="btn" value="återställ"></input>
</div>    
</fieldset>                 
</form>

phpmailer 内部验证行 338-384 示例

* Adds an address to one of the recipient arrays
* Addresses that have been added already return false, but do not throw exceptions
* @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
* @param string $address The email address to send to
* @param string $name
* @throws phpmailerException
* @return boolean true on success, false if address already used or invalid in some way
* @access protected
*/

protected function AddAnAddress($kind, $address, $name = '') {
if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
  $this->SetError($this->Lang('Invalid recipient array').': '.$kind);
  if ($this->exceptions) {
    throw new phpmailerException('Invalid recipient array: ' . $kind);
  }
  if ($this->SMTPDebug) {
    $this->edebug($this->Lang('Invalid recipient array').': '.$kind);
  }
  return false;
}
$address = trim($address);
$name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
if (!$this->ValidateAddress($address)) {
  $this->SetError($this->Lang('invalid_address').': '. $address);
  if ($this->exceptions) {
    throw new phpmailerException($this->Lang('invalid_address').': '.$address);
  }
  if ($this->SMTPDebug) {
    $this->edebug($this->Lang('invalid_address').': '.$address);
  }
  return false;
}
if ($kind != 'Reply-To') {
  if (!isset($this->all_recipients[strtolower($address)])) {
    array_push($this->$kind, array($address, $name));
    $this->all_recipients[strtolower($address)] = true;
    return true;
  }
} else {
  if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
    $this->ReplyTo[strtolower($address)] = array($address, $name);
  return true;
}
}
return false;
}

【问题讨论】:

  • 如果是protected方法,则表示不能从phpMailer外部使用。即它是供 phpMailer 类本身内部使用的。
  • 我敢建议,反思
  • @Spudley 但它确实会影响将由 phpmailer 处理以发送电子邮件的信息。几天来,我一直在尝试如何在联系表单中安全地使用 phpmailer。对于像我这样的新手来说,这似乎不是任何好的指南或说明。如果是不老的指南,它们通常不是很安全。

标签: php html forms email phpmailer


【解决方案1】:

好吧,你不能使用你发布的代码;它是 phpMailer 中的一个内部函数,它对 phpMailer 对象中的内部变量进行处理。

但是,您可以调用此代码ValidateAddress() 使用的电子邮件地址验证例程,因为它是public static 函数。

所以在你的代码中的任何地方,你都应该可以这样调用:

$valid = phpMailer::ValidateAddress($email_address);

...和$valid 将设置为truefalse,具体取决于其是否为有效地址。

但是,问题中代码的重点是,这是 PHPMailer 在内部设置其电子邮件地址的代码,并且它使用 ValidateAddress() 本身。这意味着只要你给它一个电子邮件地址,phpMailer 就已经在进行验证。你真的不需要自己先验证它,因为无论如何 phpMailer 都会为你做。

您需要做的是捕获任何 phpMailer 可能由于验证错误而返回给您的错误。默认情况下,如果函数失败,它只会返回false,所以你可以检查一下。或者,您可以打开它的异常模式并使用try...catch 来获取它抛出的异常。

不管怎样,关键是phpMailer被设计成默认是安全的;它不会接受错误的电子邮件地址或其他无效数据。所以你的问题不是真的必要;只需将数据提供给 phpMailer,它就会为您进行验证。

(这并不是说 phpMailer 是完美的代码;有人可能会发现其中的安全漏洞,就像使用任何其他软件一样,但只要您确保与补丁保持同步发布,这不应该是一个问题)

【讨论】:

  • 很好的答案,感谢您抽出宝贵的时间。
猜你喜欢
  • 1970-01-01
  • 2018-06-12
  • 2012-08-25
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
相关资源
最近更新 更多