在寻找了大约一个星期的好答案后,我意识到我必须寻求一些帮助。我一直在为是否应该使用 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;
}