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

时间:2015-04-02 21:42:56

标签: 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(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.'">';
?>

我确定它是因为我在错误的地方启动了电子邮件,但是当我尝试将其移到别处时,我会遇到各种错误。

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

2 个答案:

答案 0 :(得分:0)

你的if(count($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.'">';

答案 1 :(得分:0)

将您的邮件代码包裹在内:

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);
}