Send Forgotten Password By Mail Using PHP And MySql

Dedicated Servers from Liquid Web

Send Forgotten Password by mail using PHP and MySql is a simple script to send password to user email, if user forgets their password. They will fill in E-mail or user id, if the e-mail or database matches in our database value, then the password will be delivered to user email id.

This is a continuation for the last two scripts, this script adds functionality of retrieving forgotten password(Send Forgotten Password by mail using PHP and MySql) by the user name and sending them the login credentials by email.

  1. Simple User Registration Script in PHP and MySql
  2. Simple Login Script in PHP and mysql
  3. User Login with activation check using PHP and MySql

First we will connect to the database, and if the form is submitted, submitted values is assigned to a variable. After that we will fetch the database, if the username matches in the database, and if the results count is equal to one, then we will assign the values of password, email to variables, after that initiate the process of sending email to the user’s email address.

Below is the My Video Series on User Login, Registration Script in PHP & MySQL

Code for Send Forgotten Password by mail using PHP and MySql

If you are following from the previous scripts, then add one field in database as email, and add an email address in that field.

We will create two pages

  1. connect.php
  2. forgotpassword.php

Create connect.php file

If you are following from previous tutorials, you should have this file. Below code is to connecting database and selecting the database.

Create a Form for Forgot Password

Here we will display only username field, based on the username will fetch the data to send email.

Add styles

Here I’m using Bootstrap to add styling and some custome styles.

Save this below code as styles.css file.

PHP Logic to send Forgotten Password by E-Mail

Complete code of forgotpassword.php file

If you have problem arranging above bits of code, here is the complete code you can use.

If you want to learn other things you can read other posts – registration, login, user login with activation check, and sending forgotten password by email .

I hope this article helped you to learn user registration using PHP and MySQL . To get latest news and updates follow us on twitter & facebook, subscribe to newsletter. If you have any feedback please let us know by using comment form.

Incoming search terms:

  • forgot password code in php
  • forgot password in php
  • Forgot password php
  • forgot password php code
  • php forgot password script
  • forgot password php script
  • forget password code in php
  • php code for forgot password
  • ?gander?ttshandlingarna your password will be sent to the email address you enter
  • forgot password php code free download
Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 19 comments
Ayisha Raj


Thi$ code i$ not \/\/orking…..plea$e $end a $uitable code for the $ame

    Vivek Vengala

    Hi Ayisha Raj,

    What’s the issue, you are facing? Download entire script and try it on web server.

    Check whether mails are working in your server with simple mail function.


thank you so much its working


Thanks Mate..!!

You Really helped me out ..!!


it really worked tanks for dat bro

ranjit kumar

why not your forgetpass code is not workig

    Vivek Vengala

    Hi Ranjith, Thanks for using my code.

    Please try it on web server, it will work. Let me know the feedback.

Dennis Briggs

It’s nice that you posted the script forgotpassword.php as a whole. If you uncomment line 13 and 15 it does not show you the email or password. The script just goes blank on post.

    Vivek Vengala

    Hi Dennis,

    Thanks for letting me know.

    Dennis Briggs

    Also if there is no rows in the db it just show a blank page

      Vivek Vengala

      Yes Dennis.

      You should follow from User Registration & User Login Script. Otherwise just insert few rows in database.

        Dennis Briggs

        I added a row in the db. Row username is somename and pw and marked it active with a 1.
        If you enter a username that is not in the database the script just goes blank.(no checking)

          Vivek Vengala

          on line 35 replace the code with this line without quotes “if (empty($to)) {”

          Thanks for pointing the error, I’ll update the code soon.

Dennis Briggs

In ASP Script mine look like this
mySQL = “SELECT * FROM table WHERE email = ‘” & strEmail & “‘”

Set objRS = Server.CreateObject(“ADODB.Recordset”)
objRS.Open mySQL, StrCon

Response.Write “That email address was not found in our database. Please click Back on your browser and enter the email address you registered with.”

Dennis Briggs

This script is very nice on line 14 I added
echo “”; just for looks when checking scipt.
on line 16 I added
$email = $rows[’email’];//FETCHING EMAIL
So the script will echo the email on line 17

    Vivek Vengala

    Thats Great Dennis,

    Don’t forget to subscribe E-mail Newsletter, Like us on Facebook

    If you want to any other PHP scripts, let me know through the Contact Us page. I’ll do my best.


Warning: mail(): SMTP server response: 503 Bad sequence of commands. in C:\xampp\htdocs\quiz\login.php on line 42

    Vivek Vengala

    Hi Nitesh,

    Use this code in web server, So that emails will be delivered.

    If you are localhost, you can use phpmailer with smtp email server also will work.


Leave a Reply: