简体   繁体   中英

PHP Send Email Even If Validation Errors

I have the following code on one of my signup pages. I'm trying to figure out how to only generate the email if there are no errors...right now, it sends the email no matter what so I'm getting conflicting emails.

<?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.'">';
?>

I'm sure it's because I start the email stuff in the wrong place, but when I try to move it elsewhere, I get various errors.

Thanks in advance for any help you can provide.

Both your if(count($errors) == 0) conditions are doing exactly the same thing. You don't need to add a new condition but remove one. Your script can be simplified like this (pseudocode):

// 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.'">';

Wrap your mail code inside like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM