Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

2FA sending mails

Questions related to using nuBuilder Forte.
Post Reply
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

2FA sending mails

Unread post 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
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: 2FA sending mails

Unread post by kev1n »

Hi Johan,

Try using nuSendEmail() instead of nuSendCodeByEmail()
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

Re: 2FA sending mails

Unread post by johan »

Kev1n
Where do I adjust this?
After modifying the procedure I get internal error
Johan
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: 2FA sending mails

Unread post 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!
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

Re: 2FA sending mails

Unread post 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();
    }
    		
}

kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: 2FA sending mails

Unread post 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, "", "", []);
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

Re: 2FA sending mails

Unread post by johan »

kev1n
Problem solved. Thanks for your support.

Johan
Post Reply