如何结合表单验证和phpmailer ...? [英] how to combine form validation and phpmailer ...?
本文介绍了如何结合表单验证和phpmailer ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用PHPMailer通过电子邮件发送基本联系表单。
这个表单适合我:
<?php
$ first_name = $ _POST ['first-name'];
$ last_name = $ _POST ['last-name'];
$ email = $ _POST ['email'];
$ message = nl2br($ _ POST ['message']);
需要'PHPMailerAutoload.php';
$ mail = new PHPMailer;
// $ mail-> SMTPDebug = 3; //启用详细的调试输出
$ mail-> isSMTP(); //设置邮件使用SMTP
$ mail-> Host =''; //指定主SMTP服务器和备份服务器
$ mail-> SMTPAuth = true; //启用SMTP认证
$ mail->用户名=''; // SMTP用户名
$ mail->密码=''; // SMTP密码
$ mail-> SMTPSecure ='tls'; //启用TLS加密,`ssl`也接受
$ mail->端口= 587;
$ mail-> addReplyTo($ email,$ first_name);
$ mail-> addAddress($ email,$ first_name);
$ mail-> addAddress('blah@fake.org','Staff');
$ mail-> From ='blah@fake.org';
$ mail-> FromName ='Staff';
$ mail-> isHTML(true); //将电子邮件格式设置为HTML
$ mail-> Subject ='Hotel Room Request';
$ mail-> Body = $ message;
$ mail-> AltBody ='要查看邮件,请使用兼容HTML的电子邮件查看器!'; $!
$ b if(!$ mail-> send()){
header('location:a_url_here');
} else {
header('location:a_url_here');
$ / code $ / pre
现在我试图将它与error-检查。不知道如何结合它,仍然使它工作。这是我到目前为止所做的,并在提交后就空了。我不确定把check_input函数放在哪里,所以我把它放在底部的else部分。我如何使这个表单起作用,这样不仅可以验证用户的输入,还可以将它发送出去? <?php
$ first_name = check_input($ _ POST ['first-name']] , 请输入你的名字);
$ last_name = check_input($ _ POST ['last-name'],请输入您的姓氏);
$ email = check_input($ _ POST ['email'],请输入您的电子邮件地址);
$ message = check_input(nl2br($ _ POST ['message']),请输入您的信息);
需要'PHPMailerAutoload.php';
$ mail = new PHPMailer;
// $ mail-> SMTPDebug = 3; //启用详细的调试输出
$ mail-> isSMTP(); //设置邮件使用SMTP
$ mail-> Host =''; //指定主SMTP服务器和备份服务器
$ mail-> SMTPAuth = true; //启用SMTP认证
$ mail->用户名=''; // SMTP用户名
$ mail->密码=''; // SMTP密码
$ mail-> SMTPSecure ='tls'; //启用TLS加密,`ssl`也接受
$ mail->端口= 587;
$ mail-> addReplyTo($ email,$ first_name);
$ mail-> addAddress($ email,$ first_name);
$ mail-> addAddress('blah@fake.org','Staff');
$ mail-> From ='blah@fake.org';
$ mail-> FromName ='Staff';
$ mail-> isHTML(true); //将电子邮件格式设置为HTML
$ mail-> Subject ='Hotel Room Request';
$ mail-> Body = $ message;
$ mail-> AltBody ='要查看邮件,请使用兼容HTML的电子邮件查看器!'; $!
$ b if(!$ mail-> send()){
header('location:a_url_here');
$ b $ else b
$ data $ trim $($ data $;
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
if($ problem& strlen($ data)== 0)
{
show_error($ problem);
}
返回$ data;
}
}
?>
解决方案创建所谓的验证器 class:
class Validator {
//验证程序
//语法:< field-name> => '< list-of-rules,joined-with-pipe>',
protected $ rules = [$ b $'first-name'=> 'required',
'last-name'=> 'required',
'message'=> 'required',
'email'=> 'required | email',
];
//显示具体的字段规则是否失败的消息
//:field将被字段实际名称替换
protected $ messages = [
'需要'=> 'Field:field is required',
'nonzero'=> '字段:字段不能为零'
'email'=> 'Field:field must represent a emai address'
];
保护$错误;
//调用它来验证提供的$ input
函数validate($ input){
$ errors = [];
//为每个已定义的字段规则集
foreach($ this->规则为$ field => $ rules){
$ rules = explode('|' ,$ rules);
//对于每个规则
foreach($ rules as $ rule)
//带名称checkNameofrule的调用函数
if(!$ this-> {check
//记忆错误
$ errors [$ field] [] = $ this->>错误($ field,$规则);
}
//验证通过,如果没有错误
return!($ this-> errors = $ errors);
函数errors(){
return $ this-> errors;
函数错误($ field,$ error){
return str_replace(':field',$ field,$ this-> messages [$ field]);
//检查必填字段
函数checkRequired($ input,$ field){
if(!isset($ input [$ field]))
返回false;
return trim(htmlspecialchars(stripslashes($ input [$ field])))!='';
}
//检查有效的电子邮件
函数checkEmail($ input,$ field){
return !! preg_match('#。+ @ [^。 ] + \ .. +#',@ $ input [$ field]);
}
//其他自定义检查
函数checkNonzero($ input,$ field){
return intval(@ $ input [$ field])!= 0 ;
}
}
像这样使用它:
$ validator = new Validator();
//验证...
if(!$ validator-> validate($ _ POST)){
//看起来输入错误
echo< div class = \errors \>;
echo< b>看起来您在输入中有错误:< / b>< br />;
foreach($ validator-> errors()as $ field => $ errors){
foreach($ errors as $ error)
echo< p> {$ error} < / p>中;
}
echo< / div>;
} else {
//输入已通过验证,发送电子邮件...
要求'PHPMailerAutoload.php';
$ mail = new PHPMailer; $!
$ b ...
if(!$ mail-> send()){
header('location:a_url_here');
} else {
header('location:a_url_here');
}
}
I am trying to email a basic contact form using PHPMailer.
This form works for me:
<?php
$first_name = $_POST['first-name'];
$last_name = $_POST['last-name'];
$email = $_POST['email'];
$message = nl2br($_POST['message']);
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->addReplyTo( $email, $first_name );
$mail->addAddress( $email, $first_name );
$mail->addAddress( 'blah@fake.org', 'Staff' );
$mail->From = 'blah@fake.org';
$mail->FromName = 'Staff';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hotel Room Request';
$mail->Body = $message;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
if(!$mail->send()) {
header('location: a_url_here');
} else {
header('location: a_url_here');
}
Now, I'm trying to combine it with error-checking. Don't know how to combine it and still make it work. This is what I have so far, and it blanks out upon submittal. I wasn't sure where to put check_input function, so I put it in the else part at the bottom. How do I make this form functional so not only does it validate user's input but email it out?
<?php
$first_name = check_input($_POST['first-name'], "Please enter your name");
$last_name = check_input($_POST['last-name'], "Please enter your last name");
$email = check_input($_POST['email'], "Please enter your email address");
$message = check_input(nl2br($_POST['message']), "Please enter your message");
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->addReplyTo( $email, $first_name );
$mail->addAddress( $email, $first_name );
$mail->addAddress( 'blah@fake.org', 'Staff' );
$mail->From = 'blah@fake.org';
$mail->FromName = 'Staff';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hotel Room Request';
$mail->Body = $message;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
if(!$mail->send()) {
header('location: a_url_here');
} else {
function check_input($data, $problem = ' ')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
}
?>
解决方案 Create so-called validator
class:
class Validator {
// set of rules for validator
// syntax: <field-name> => '<list-of-rules, joined-with-pipe>',
protected $rules = [
'first-name' => 'required',
'last-name' => 'required',
'message' => 'required',
'email' => 'required|email',
];
// message to show if concrete field-rule failed
// ":field" will be replaced with field actual name
protected $messages = [
'required' => 'Field :field is required',
'nonzero' => 'Field :field must not be zero'
'email' => 'Field :field must represent an emai address'
];
protected $errors;
// call this to validate provided $input
function validate($input) {
$errors = [];
// for each defined field-ruleset
foreach ($this->rules as $field => $rules) {
$rules = explode('|', $rules);
// for each rule
foreach ($rules as $rule)
// call function with name "checkNameofrule"
if (!$this->{"check" . ucfirst($rule)}($input, $field))
// memorize error, if any
$errors[$field][] = $this->error($field, $rule);
}
// validation passed if there are no errors
return !($this->errors = $errors);
}
function errors() {
return $this->errors;
}
function error($field, $error) {
return str_replace(':field', $field, $this->messages[$field]);
}
// check for required fields
function checkRequired($input, $field) {
if (!isset($input[$field]))
return false;
return trim(htmlspecialchars(stripslashes($input[$field]))) != '';
}
// check for valid email
function checkEmail($input, $field) {
return !!preg_match('#.+@[^.]+\..+#', @$input[$field]);
}
// other custom checks
function checkNonzero($input, $field) {
return intval(@$input[$field]) != 0;
}
}
And use it like this:
$validator = new Validator();
// validating...
if (!$validator->validate($_POST)) {
// looks like there are errors in input
echo "<div class=\"errors\">";
echo "<b>Looks like you have errors in input:</b><br />";
foreach ($validator->errors() as $field => $errors) {
foreach ($errors as $error)
echo "<p>{$error}</p>";
}
echo "</div>";
} else {
// input had passed validation, send email...
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
...
if(!$mail->send()) {
header('location: a_url_here');
} else {
header('location: a_url_here');
}
}
这篇关于如何结合表单验证和phpmailer ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文