ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

How to Email Address confirmation / Verification in PHP

Updated on November 9, 2011


In this tutorial, We will develop some php functions which are foremost tasks in email confirmation script. Email confirmation is the most easiest method,

  1. to verify person's identity,
  2. to verify whether registrant is human or machine,
  3. to just simply verify it's email for future conversations, contacts etc.

It is very easy to develop email confirmation script in php. You need not to be a professional for such task. Below are the steps that need to be followed while developing such script.

Steps TO Follow

  1. First, Generate and store activation key and activation status in database while registration,
  2. Secondly, Send an email to registrant's email ID containing activation link which will take user to activation page on our site,
  3. Activation link should contain at-least two parameters in URL, One holding registrant's 'email ID' and second one holding an 'activation key' associated with that ID. For example, "".
  4. When, User will land on our activation page, we will fetch those parameters from the activation link and will try finding out such combination in our database,
  5. And, If such combination found, We will set activation status of associated user to active in our database.
  6. And Finally, Email has been Confirmed.

Generating Activation Key

Well, It is easy to generate any random key but we will try generating an unique key by using email address of user, See the PHP function shown below that can be used for this purpose.

PHP Function to Generate Activation KEY

function gen_activation_key($email){	
	$generatedKey = sha1(mt_rand(10000,99999).time().$email);
	return $generatedKey;

Sending Confirmation Email to User

Now, after generating and storing 'Activation Key' in our database, We need to send an email to registrant's Email ID containing a link pointing to our activation page. The link should contain two type of parameters,

  2. key=(activation key), without braces.

See the PHP function below, which would send an confirmation email to registrant (Do changes as per your requirements).

PHP Function for Sending Confirmation Email

function verificationMAIL($gKEY,$email){
	$subject='Verify your Email Address - EXAMPLE.COM';
	$headers .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$mailMsg='<h2>Verify Your Email Address</h1><br />
				<a href="'.$email.'&key='.$gKEY.'">Click Here to Verify.</a>';

Confirm and Activate User

Now, When User will land on our activation page after clicking on activation link, We will fetch those email and key combination from URL and will try finding out such combination in our database. If found one, We'll set Activation status of associated user to 'Active'.

PHP Function to Confirm and Activate User

function verifyEmailAddress($email, $vkey){
		$query=mysql_query("SELECT * FROM users WHERE email='$email' AND activation_key='$vkey'");
		if($fetch['activation_status'] == 'Active'){
			$output='Your Email has already been verified.';
				$updateQ=mysql_query("UPDATE users SET activation_status='Active' WHERE email='$email' AND activation_key='$vkey'");
					$output='Email Verified';
					$output='System Faced an error while updating your status.';
				$output='Unable to verify your email address.';
		return $output;

Important information about msConnect() function

msConnect() function above is actually a single custom build function (Doesn't exist in PHP functions Library) for connecting to and selecting a database from MySQL, You will be required to create similar one, Click here for more information.


    0 of 8192 characters used
    Post Comment

    • profile image

      Max 3 years ago

      Be precise, where do we put these codes? register.php or activate.php?

    • profile image

      Florin 4 years ago


      Could you upload a example script that using this validation?

    • profile image

      Mr Akheel 4 years ago

      Can i use this Email service in Localhost Server...?

    • itech profile image

      Krishna 6 years ago from New Delhi, India






      You can paste this in your script before using

      verifyEmailAddress($email, $vkey) function.

    • profile image

      David Kirui 6 years ago

      ok how do I fetch the vkey from the URL?

    • profile image

      sebastien paquet 6 years ago

      that was fast! Thank you very much.

    • itech profile image

      Krishna 6 years ago from New Delhi, India

      Thanks for Commenting Sebastien...

      There is a little difference in $generatedKey and $gKEY i.e. $generatedKey (Variable) Scope is limited to function itself, therefore, it can't be accessed outside gen_activation_key() function AND $gKey is an parameter of verificationMAIL() function.

      In Short: " $gKey=gen_activation_key($email); ".

      $vKey is "Verification Key" which will fetch KEY from URL Parameter, When User will land on our webpage after clicking on verification link.

    • Sebastien Paquet profile image

      Sebastien Paquet 6 years ago

      thanks man, that's gonna be useful. I'm building a voting system. But one question I have is: You have the variable $generatedKey and $gKey, is that supposed to be the same one? and what about vKey? I'm confused.. thanks.