Category: Email

Sending Mail

Sending mail using PHP is very simple. Here I’ll show how we can send the activation mail to a registered using along with the verification key we generated in last post.

    $emailto= "";
    $subject	= "";
    $min_size	= "1";
    $max_size	= "4000";
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .="To:".$emailto."<".$emailto.">\r\n";
    $headers .="From:".$frmname."<".$email.">\r\n";
    $headers 	= "From:";
    $message	= "Registration Successfull!!\n\nThank you for registering with us. \n\nUser name: ". $username . "\nPassword: ". $password ."\n\n Click here for activation.\n".$pass;
   $ok = @mail($emailto, $subject, $message, $headers);
   $msg	= "Registered Successfully";

This will send the recipient with the username and password along with the verification key generated instructing them to click on the link to activate their profile. We will see how to implement the activation page in this post. We also need to understand how to first add the generated key to database.

Actually adding the generated key to database is as easy as any other PHP-MySQL queries. I’ll just give an example here.

$qry = "INSERT INTO users (name, password, email, vkey) VALUES ('$uname', '$passwd', '$email', '$pass');

We’ll be having a field in the ‘users‘ table named ‘status‘ which is by default ‘N’. The purpose of the activate page is set this to ‘Y‘. It is really very easy. Here is the code in the ‘activate.php‘ page.

$verikey = $_GET['verikey'];
 $qry = "UPDATE users SET status = 'Y' WHERE vkey = '$verikey'";

While registering with a website we often need to verfy our email id. Given below is the code to develop that functionality. The following will generate the verification key which has 6 characters

function generatePassword()
    $length =6;
    $validchars = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    $password ="";
    $counter = 0;

    while($counter < $length)
        $actChar = substr($validchars, rand(0, strlen($validchars)), 1);

        if(!strstr($password, $actchar))
           $password .= $actChar;

   return $password;

rand(0, stlen($validchars)) – generates a random number between 0 and the length of $validchars (inclusive of both ends).

substr($a, $b , len) – get generates a sub string from $a from the first occurrence of $b to length specified by len.

strstr($a, $b) – checks whether $a has $b in it. This is done to create unique characters.

So this is how you generate random string. How you can use it for verification purpose of a user registration is explained in the subsequent posts.