email to retrieve a password - Forum

Forum Navigation
You need to log in to create posts and topics.

email to retrieve a password

Hello dear friends!
I would like to give the possibility to users of an application who have created an account and who have forgotten their password, to receive an email in their inbox containing a link to a page to create a new password. Easy to do?
Thanks to all of you!

I've been going around in circles for two days...and I'm starting to tear my hair out :)

@cdy44-2 if you are using neoPhp Plugin for users management, keep in mind that passwords are stored hashed (encrypted).
Take a close look at neofunctions.php code to understand how it works. You should send username (or user id) and password to a new .php script and hash the password before updating the database row.

//Get the username and password from the submited form
$theUserName=$_POST['username']
$theUserPassword=$_POST['password']
//Hash the password
$theUserPassword=password_hash($theUserPassword, PASSWORD_DEFAULT);

Then open the database (procedure depends on your database) and execute this SQL:

UPDATE neousers SET password = '$theUserPassword' WHERE username='$theUserName';

By checking the neofunctions.php code you will find how to open the database and execute the SQL query.
I hope it helps

Regards.

 

@cdy44-2 you can also try to hash the string using JavaScript before sending it to the server. This way it's not necessary to write a new .php script as you can use neoPhpExecSql as usual with the UPDATE SQL query:

UPDATE neousers SET password = ? WHERE username= ?;

How to hash a string using Jasvascript:
https://www.geeksforgeeks.org/how-to-create-hash-from-string-in-javascript/

Regards

Thank Luis for this answer.

The problem is that I dont know how I can send an email containing an URL link ( I think use the 'senddata.php' script like to send a form), but I don't see how to enclose the URL link in my form...

Best regards,

@cdy44-2 you should modify the script a little bit so it sends HTML instead of plain text:

<?php 
//Header required when app and php are of different origins 
header("Access-Control-Allow-Origin: *"); 
$message="";
if($_SERVER["REQUEST_METHOD"] === "POST"){
     foreach($_POST as $key => $value){
          $message .= "".htmlspecialchars($key).": ".htmlspecialchars($value)."\r\n";
     }
     $to = "somebody@example.com";
     $subject = "My subject";
     $headers = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
     $headers .= "From: webmaster@example.com" . "\r\n" . "CC: somebodyelse@example.com";
     mail($to,$subject,$message,$headers);
     echo "EMAIL SUCCESSFULLY SEND";
}else{
     $result = "INVALID DATA";
     echo $result;
}
?>

The headers are the key.
Now you can send HTML in your email message:

<a href="https://link.to/your/website">Click here to reset your password</a>

I think this will work.

Ok, but where put I the : <a href="https://link.to/your/website">Click here to reset your password</a> ?

Sorry to ask so basic questions...

Hi Luis,
I tried different places, no way to make it working...
I need your help. ;)

Best regards,

Oups ! I mean places to write : <a href="https://link.to/your/website">Click here to reset your password</a>

@cdy44-2 To help you I need more information about what are you trying to achieve.
'senddata.php' script is designed to get information from a Form Object and send it to an email. So your HTML link should be sent from a Text Input or Text Area Object located inside a Form Object. But you can easily modify the script to send a predesigned content instead.

Regards.

@luishp, yes it is precisaly that, a predesigned content.
Regards,

<?php 
//Header required when app and php are of different origins 
header("Access-Control-Allow-Origin: *"); 
$message="<a href='https://yourdomain.com/yourpath/'>Click here to reset your password</a>";
if($_SERVER["REQUEST_METHOD"] === "POST"){
     $to = "somebody@example.com";
     $subject = "My subject";
     $headers = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
     $headers .= "From: webmaster@example.com" . "\r\n" . "CC: somebodyelse@example.com";
     mail($to,$subject,$message,$headers);
     echo "EMAIL SUCCESSFULLY SEND";
}else{
     $result = "INVALID DATA";
     echo $result;
}
?>

@cdy44-2 try this.

Hi @luishp,

I tried your sugestion but can't make it work...

<?php 
//Header required when app and php are of different origins 
header("Access-Control-Allow-Origin: *"); 
$message="<a href='https://yourdomain.com/yourpath/'>Click here to reset your password</a>";
if($_SERVER["REQUEST_METHOD"] === "POST"){
     foreach($_POST as $key => $value){
         $message .= "".htmlspecialchars($key).": ".htmlspecialchars($value)."\r\n";
     }
     
 $to = $_POST['targetEmail'];
     $subject = "My subject";
     $headers = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
     $headers .= "From: webmaster@example.com" . "\r\n" . "CC: somebodyelse@example.com";
     mail($to,$subject,$message,$headers);
     echo "EMAIL SUCCESSFULLY SEND";
}else{
     $result = "INVALID DATA";
     echo $result;
}
?>

 

Uploaded files:
  • You need to login to have access to uploads.

I have just changed the ligne " $to = "somebody@example.com";" by this "$to = $_POST['targetEmail'];"

By the way, I have just start to follow  PHP formation...

@cdy44-2 I have added this code to the Form Object "success" tab:

ConsoleLog [data]

...and I get a "EMAIL SUCCESSFULLY SENT" message on the web browser Console, but the email never comes.
Are you using webmaster@example.com as the sender email? You should only be allowed to send messages from an existing address at your own domain. Most web servers will reject any other email address.

CDY@44 has reacted to this post.
CDY@44

@luishp, That was the problem ! I changed it and now it works !! Thank you !!!

Great! :)

CDY@44 has reacted to this post.
CDY@44