Ste*_*enW 2 html php forms email phpmailer
在寻找一个好的答案大约一个星期后,我意识到我不得不寻求一些帮助.我一直在努力,如果我应该使用PHP邮件或Swiftmailer一周,我决定使用PHP邮件,因为我觉得文档更全面,并且有更多的例子.在查看代码后,我发现PHP邮件程序具有许多内置的卫生/验证功能.我想知道是否有人看到过任何缺点或漏洞?你会怎么用它们?为了让问题更具实质性,我想知道如何使用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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
Spu*_*ley 11
好吧,你不能使用你发布的代码; 它是phpMailer中的一个内部函数,用于处理phpMailer对象中的内部变量.
但是,您可以调用此代码使用的电子邮件地址验证例程,ValidateAddress()因为它是一个public static函数.
因此,在代码中的任何位置,您都应该可以调用以下内容:
$valid = phpMailer::ValidateAddress($email_address);
Run Code Online (Sandbox Code Playgroud)
...并$valid会被设置为true或false取决于其是否有效地址.
但是,问题中代码的重点是,这是PHPMailer在内部设置其电子邮件地址的代码,并且它正在使用ValidateAddress()它自己.这意味着只要您给它一个电子邮件地址,phpMailer就已经在进行验证了.你真的不需要先自己验证它,因为phpMailer无论如何都会为你做这件事.
你需要做的是捕获由于valdation错误而导致phpMailer可能抛出的任何错误.默认情况下,false如果函数失败,它只会返回,因此您可以检查它.或者,您可以打开其例外模式并使用try...catch它来获取它抛出的异常.
无论哪种方式,重点是phpMailer默认设计为安全; 它不会接受错误的电子邮件地址或其他无效数据.所以你的问题不是必要的; 只需给phpMailer数据,它就会为你做验证.
(这并不是说phpMailer是完美的代码;有人可能会在其中发现安全漏洞,就像任何其他软件一样,但只要你确保与补丁版本保持同步,那就是不应该是一个问题)