Sending a confirmation email using php and mysql
Posted in Linux, mysql, php on April 30th, 2011 by admin – Be the first to commentThe important part is the logical part. Because coding is nothing. First we should look at what’s going on there. When we register on a website we have to submit name, user name, password, address etc. in the registration form. After we click submit button it appears a thing like this. “Your confirmation link has been sent to your email address”. To activate the registration we have to follow that link. That’s the simple steps anyone can see from the out side.
Behind the screen logic part is like this.
One user register on the registration page and generate a random code.
The keep the data which user has entered in a temporary table. And send the confirmation link through an email.
When the user click on the confirmation link the data move in to a permanent table and keep safe.
That’s the simple logic.
First we have to create a registration form. I use this simple registration form.

Then we have to use a database and create relevant tables.
One table with fields of code, name, email, password.

Other table with id, name, email, password.


After user submit details, page redirect to a page called process.php . In there it generates a random code, insert data into the temp_users table and send the confirmation email to the user.
<?
include('db.ini');
$temp="temp_members";
//retrieve data from the registration form
$code=md5(uniqid(rand()));
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
// Insert data into the temp_users table
$sql="INSERT INTO $temp(code, name, email, password)VALUES('$code', '$name', '$email', '$password')";
$result=mysql_query($sql);
if($result){
// ---------------- SEND MAIL FORM ----------------
$to=$email;
$subject="Your confirmation link here";
$header="from: your name ";
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="http://www.myweb.com/confirmation.php?key=$code";
// send email
$sentmail = mail($to,$subject,$message,$header);
}
else {
echo "Not found your email";
}
if($sentmail){
echo "Your confirmation link has been sent to your email address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}
?>

When the user click the confirmation link it redirect to a page called confirmation.php . In there it checks the code with the database. If the code is match with the code with the table, it moves that row to the registered_users table. And delete the code from the temp_users table.
<?
include('db.ini');
$key=$_GET['key'];
$temp_members="temp_members";
// comare the key with the table data
$sql1="SELECT * FROM $temp_members WHERE code ='$key'";
$result1=mysql_query($sql1);
// If key matched
if($result1){
$count=mysql_num_rows($result1);
// if found the key in the database, retrieve data from the temp_members_db
if($count==1){
$rows=mysql_fetch_array($result1);
$name=$rows['name'];
$email=$rows['email'];
$password=$rows['password'];
$registered="registered_members";
// Insert data from temp_members_db to registered_members
$sql2="INSERT INTO $registered(name, email, password)VALUES('$name', '$email', '$password')";
$result2=mysql_query($sql2);
}
// if key is not found
else {
echo "Confirmation code is incorrect";
}
// After the inserting the data to registered_members table then activate the account
if($result2){
echo "Your account has been activated";
//Delete the relevent data from the temp_users table
$sql3="DELETE FROM $temp_members WHERE code = '$key'";
$result3=mysql_query($sql3);
}
}
?>
And this works fine for me many times. And here’s the db.ini file.
<?
$host=""; // Host name
$username=""; // username
$password=""; // password
$db_name=""; // Database name
//Connect to the server
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
?>

