即使验证错误,PHP也会发送电子邮件

即使验证错误,PHP也会发送电子邮件,php,forms,validation,Php,Forms,Validation,我的注册页面上有以下代码。我正试图找出只有在没有错误的情况下才生成电子邮件的方法…现在,不管发生什么,它都会发送电子邮件,所以我收到了相互冲突的电子邮件 <?php require_once("models/config.php"); if (!securePage($_SERVER['PHP_SELF'])){die();} //Prevent the user visiting the logged in page if he/she is already logged in if

我的注册页面上有以下代码。我正试图找出只有在没有错误的情况下才生成电子邮件的方法…现在,不管发生什么,它都会发送电子邮件,所以我收到了相互冲突的电子邮件

<?php

require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}

//Prevent the user visiting the logged in page if he/she is already logged in
if(isUserLoggedIn()) { header("Location: account.php"); die(); }

//Forms posted
if(!empty($_POST))
{
    $errors = array();
    $email = trim($_POST["email"]);
    $username = trim($_POST["username"]);
    $displayname = trim($_POST["displayname"]);
    $password = trim($_POST["password"]);
    $confirm_pass = trim($_POST["passwordc"]);
    $captcha = md5($_POST["captcha"]);


    if ($captcha != $_SESSION['captcha'])
    {
        $errors[] = lang("CAPTCHA_FAIL");
    }
    if(minMaxRange(4,25,$username))
    {
        $errors[] = lang("ACCOUNT_USER_CHAR_LIMIT",array(4,25));
    }
    if(!ctype_alnum($username)){
        $errors[] = lang("ACCOUNT_USER_INVALID_CHARACTERS");
    }
    if(minMaxRange(4,60,$displayname))
    {
        $errors[] = lang("ACCOUNT_DISPLAY_CHAR_LIMIT",array(4,60));
    }
    if(minMaxRange(4,50,$password) && minMaxRange(4,50,$confirm_pass))
    {
        $errors[] = lang("ACCOUNT_PASS_CHAR_LIMIT",array(4,50));
    }
    else if($password != $confirm_pass)
    {
        $errors[] = lang("ACCOUNT_PASS_MISMATCH");
    }
    if(!isValidEmail($email))
    {
        $errors[] = lang("ACCOUNT_INVALID_EMAIL");
    }
    //End data validation
    if(count($errors) == 0)
    {   
        //Construct a user object
        $user = new User($username,$displayname,$password,$email);

        //Checking this flag tells us whether there were any errors such as possible data duplication occured
        if(!$user->status)
        {
            if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
            if($user->displayname_taken) $errors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
            if($user->email_taken)    $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));       
        }
        else
        {
            //Attempt to add the user to the database, carry out finishing  tasks like emailing the user (if required)
            if(!$user->userCakeAddUser())
            {
                if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
                if($user->sql_failure)  $errors[] = lang("SQL_ERROR");
            }
        }
    }
    if(count($errors) == 0) {
        $successes[] = $user->success;
    }
}
echo resultBlock($errors,$successes);

$to = 'myemail@domain.com';
$subject = 'New User Signup';
$url = 'mydomain.com/account.php'; 
$headers .= "From: myemail@domain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message .= '<html><body>';
$message .= "<p>...message contents...</p>";

mail($to, $subject, $message, $headers);

echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';
?>
我确信这是因为我在错误的地方开始发送电子邮件,但是当我尝试将它移到其他地方时,我会遇到各种错误

提前感谢您提供的任何帮助

两个ifcount$errors==0条件所做的事情完全相同。您不需要添加新条件,只需要删除一个。您的脚本可以简化为以下伪代码:

// some processing
// ...

//Forms posted
if(!empty($_POST))
{
    // filling $errors[]

    //End data validation
    if(count($errors) == 0)
    {
        //Construct a user object
        // ...

        //Checking this flag tells us whether there were any errors such as possible data duplication occured
        // ...

        $successes[] = $user->success;

        // Send email if no error
        // ...
        mail($to, $subject, $message, $headers);
    }
}

echo resultBlock($errors,$successes);

$url = 'mydomain.com/account.php';

echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';

将您的邮件代码按如下方式包装:

if(count($errors) == 0) {
    $to = 'myemail@domain.com';
    $subject = 'New User Signup';
    $url = 'mydomain.com/account.php'; 
    $headers .= "From: myemail@domain.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $message .= '<html><body>';
    $message .= "<p>...message contents...</p>";
   mail($to, $subject, $message, $headers);
}

您的邮件功能超出了ifcount$errors==0条件。您需要将电子邮件部分置于ifcount$errors==0条件内。我还建议使用诸如PHPMailer之类的邮件系统,而不是依赖邮件功能。您将收到许多未送达的电子邮件,因为电子邮件将来自“apache”@lukas@drew。有两个ifcount$errors==0的实例,我将其放在哪个实例中有关系……还是应该在右括号前添加第三个实例?您可以简化错误条件。看看我的答案。