简体   繁体   中英

Confirmation email not sending

so i have this code. It is for a site that when an admin approves you to be an user he clicks this link. It wil set the account to active, but I also want is to send an email.

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require('includes/config.php');

//collect values from the url
$memberID = trim($_GET['x']);
$active = trim($_GET['y']);

//if id is number and the active token is not empty carry on
if(is_numeric($memberID) && !empty($active)){

//update users record set the active column to Yes where the memberID and active value match the ones provided in the array
$stmt = $db->prepare("UPDATE members SET active = 'Yes' WHERE memberID = :memberID AND active = :active");
$stmt->execute(array(
    ':memberID' => $memberID,
    ':active' => $active,
));

//if the row was updated redirect the user
if($stmt->rowCount() == 1){
//redirect to login page
$stmt = $db->prepare("SELECT email FROM members WHERE memberID = :memberID");
$stmt->bindparam(':memberID', $memberID);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);
//send email
    $to = $result->email;
    $subject = "account actief";
    $body = "Uw account op http://www.nol-groningen.nl is actief \n\n
    Met Vriendelijke groet,\n\n
    Website Admin";
    $additionalheaders = "From: <".SITEEMAIL.">\r\n";
    $additionalheaders .= "Reply-To: $".SITEEMAIL."";
    mail($to, $subject, $body, $additionalheaders);

header('Location: login.php?action=active');
exit;

} else {
echo "Your account could not be activated."; 
}

}
?>

It did work before i put the email stuff in, now it just goes to a blank page. Does anyone know how i can fix it and let it send an confirmation email?

You are redirecting page before the email code runs so

move header('Location: login.php?action=active'); down and place it before exit;

 header('Location: login.php?action=active');
 exit;

change your if statement like this

if($stmt->rowCount() == 1){
    //redirect to login page
    $stmt = $db->prepare("SELECT email FROM members WHERE memberID = :memberID");
    $stmt->bindparam(':memberID', $memberID);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_OBJ);
    //send email
        $to = $result->email;
        $subject = "account actief";
        $body = "Uw account op http://www.nol-groningen.nl is actief \n\n
        Met Vriendelijke groet,\n\n
        Website Admin";
        $additionalheaders = "From: <".SITEEMAIL.">\r\n";
        $additionalheaders .= "Reply-To: $".SITEEMAIL."";
        mail($to, $subject, $body, $additionalheaders);

    header('Location: login.php?action=active');
    exit;

} else {
    echo "Your account could not be activated."; 
}

Aren't there a PHP error in :

$stmt->execute(array(
':email' => $email,

//send email
    $to = $email;
    $subject = "account actief";
    $body = "Uw account op http://www.nol-groningen.nl is actief \n\n
    [...]
));

Shouldn't you check for the answer and send email out of the execute ?

There are a variety of reasons your script appears to not be sending emails. It's difficult to diagnose these things unless there is an obvious syntax error. Without one you need to run through the checklist below to find any potential pitfalls you may be encountering.

answered Jul 9 at 2:21 by John Conde

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