Page 1 of 1

2FA sending mails

Posted: Thu Mar 27, 2025 3:49 pm
by johan
Hi

When I enable 2FA, I do not receive any e-mails. Even though the popup says the email will be sent.
When I test my e-mail settings in the settings, I do receive an e-mail.
Any idea where it's going wrong?

Johan

Re: 2FA sending mails

Posted: Thu Mar 27, 2025 3:55 pm
by kev1n
Hi Johan,

Try using nuSendEmail() instead of nuSendCodeByEmail()

Re: 2FA sending mails

Posted: Thu Mar 27, 2025 4:30 pm
by johan
Kev1n
Where do I adjust this?
After modifying the procedure I get internal error
Johan

Re: 2FA sending mails

Posted: Thu Mar 27, 2025 4:45 pm
by kev1n
I didn't find any errors in your code.

I also asked the AI but it wasn't helpful :o
Oh wow, what a fun challenge! I made a mystery change in my code, and now it throws an error. Your task, should you choose to accept it, is to magically deduce what went wrong—without seeing the code, of course. Bonus points if you fix it with telepathic debugging skills!

Re: 2FA sending mails

Posted: Thu Mar 27, 2025 6:43 pm
by johan
Kev1n
I used 2fa as descriped in https://wiki.nubuilder.cloud/index.php? ... tion_-_2FA

I modified procedure

Code: Select all

function nuGetEmail($adminEmail = '') {
    $u = nuUser();
    $email = $u->sus_email;
    if ($email == null) $email = $adminEmail;
    return $email;
} 

function nuSendCodeByEmail($code) {
    $content = 'Uw code: '.$code;
    $subject = '2FA';
    $fromName = '####';
    $sendTo = nuGetEmail('mymail'); // Pass the globeadmin email address here 
    nuEmailPHP($sendTo, $fromAddress, $fromName, $content, $subject);
    nuDisplayError("Er werd een email verzonden. Onderwerp = '".$subject."'");
}

$command = ! isset($nuauthcommand) ? "#nuauthcommand#" : $nuauthcommand;

$u		= (nuHash()['GLOBAL_ACCESS'] == '1'  ? $_SESSION['nubuilder_session_data']['GLOBEADMIN_NAME'] : nuUser()->zzzzsys_user_id);

if ($command == 'auth_check') {                                                 // Check if the token is valid
    
    $auth2FACheck = $nuAuthCheck(get_defined_vars(), 3600);                     // Token validated less than 1 hour ago

} elseif ($command == 'send') {                                                 // Generate and send the token 
    
    $token = nuGenerateToken(7);                                                // Generate a random string of length 7
    
    nuSet2FAToken($token);                                                      // Store the token in the session
    nuSet2FAVerifiedTime();			
    //nuOutput2FATokenToConsole($token);                                          // For testing purposes, output the token to the developer console

  nuSendCodeByEmail($token);                                                // Send the token by email. 
    
} elseif ($command == 'verify') {                                               // Verify if the entered token is valid

   //  next line is causing an error
     $tokenExpired = nuAuthGetElapsedTime(nuGet2FATokenSentTime($u)) > 300;     // Sent token valid for 5 min (300 s)
  
    if (nuTokenMatches("#auth_code_verify#", $u) && ! $tokenExpired) {          // Entered token matches and token is not expired
        nuSetAuthenticated("#auth_code_verify#");
    } else {
        nuShow2FAAuthenticationError();
    }
    		
}


Re: 2FA sending mails

Posted: Thu Mar 27, 2025 7:48 pm
by kev1n
kev1n wrote: Thu Mar 27, 2025 3:55 pm Hi Johan,

Try using nuSendEmail() instead of nuSendCodeByEmail()
I meant replace nuEmailPHP() with nuSendEmail(), adjusting the parameters as needed.

E.g.

Code: Select all

nuSendEmail($sendTo, $fromAddress, $fromName, $content, $subject, [], false, "", "", []);

Re: 2FA sending mails

Posted: Thu Mar 27, 2025 8:00 pm
by johan
kev1n
Problem solved. Thanks for your support.

Johan