I am trying to send email which contains password of the user. i want to retrieve email id from database. and my password field and email field are in different tables.
I have tried but mail is not going to the email id.
Here is the code:
HTML:

            UserName: <input type="text" name="txt_usrnm"/>
            Security Ques:<select name="sec_ques_drpdwn">

                    <option>What is your academy's first number?</option>
                    <option>what is your nick name?</option>
                    <option>What is your favourite colour?</option>
                    <option>What is your pet name?</option>
                </select>
            Security Ans:<input type="text" name="txt_sec_ans"/>
        <input type="submit" name="submit" value="Submit" align="middle" style="width:auto" />

PHP code:

    <?php

    if(isset($_POST['submit']))
    {
        $usrnm=$_POST['txt_usrnm'];
        $sec_ques=$_POST['sec_ques_drpdwn'];
        $sec_ans=$_POST['txt_sec_ans'];


        $user_name = "root";
        $password = "";
        $database = "show_your_talent";
        $server = "127.0.0.1";
        $db_handle = mysql_connect($server, $user_name, $password);
        $db_found = mysql_select_db($database, $db_handle);

        if ($db_found) 
        {
            //print "Database Found ";
            $substr=substr($usrnm,0,2);

            if($substr=="AC")
            {
                $res="SELECT * FROM reg_ac WHERE UserName='".$usrnm."' and SecurityQues ='".$sec_ques."' and SecurityAns='".$sec_ans."'";
                $res2="select Password from username where UserName='".$usrnm."'";

            }
            else
            {
                $res="SELECT * FROM reg_indi WHERE UserName='".$usrnm."' and SecurityQues ='".$sec_ques."' and SecurityAns='".$sec_ans."'";
                $res2="select Password from username where UserName='".$usrnm."'";
            }




$result = mysql_query($res);
$result2 = mysql_query($res2);
$count=mysql_num_rows($result);
    if($count==1)
    {   
        echo "count";
        while ($row = mysql_fetch_array($result) && $row2 = mysql_fetch_array($result2)) 
        {
            echo "while";
            $from = "Name <hmparikh15@gmail.com>"; 
            $header = "From: ".$from; 
            //$header = "Reply-To: ".$from."rn"; 
            $to = $row['E-mail']; 
            $subject = "Password Recovery "; 
            $emailBody = "UserName ".$row['UserName']." 
            Password ".$row2['Password']." \n"; 
            echo $emailBody;
            echo $to;
        }


        if (mail($to, $subject, $emailBody, $header)) 
        { 
            echo "if mail";
            echo "<script type='text/javascript'> alert('Password has been sent to your email id.')</script>";
        }
        else
        {
            echo "Email not sent. Please, try again after some time.";
        }
    }
    else
    {
        echo "<script type='text/javascript'> alert('Wrong Question/Answer')</script>";
    }



mysql_close($db_handle);

        }
    }

    ?>

Its printing the message : Password has been sent to your email id.
But its not sending the email.

Please help me.

Recommended Answers

All 2 Replies

Check row number 49. $to = $row['E-mail'];

Your message printing but the code is not getting proper email address from the database.

Check your output is correct based on your querry you are applying.

Why not join your two tables if the primay index is the username for both.

I haven't tested this code out but in theroy it should work.

if( $substr=="AC" )
{
    $query = "SELECT user_tb.*, pass_tb.Password FROM reg_ac, username 
              WHERE user_tb.UserName = pass_tb.UserName AND user_tb.UserName = '$usrnm' 
              AND user_tb.SecurityQues = '$se_ques' AND user_tb.SecurityAns = '$sec_ans' LIMIT 1";
}
else
{
    $query = "SELECT user_tb.*, pass_tb.password as password FROM reg_indi, username 
              WHERE user_tb.UserName = pass_tb.UserName AND user_tb.UserName = '$usrnm' 
              AND user_tb.SecurityQues = '$se_ques' AND user_tb.SecurityAns = '$sec_ans' LIMIT 1";
}


$results = mysql_query( $query );


if( mysql_num_rows( $results ) == 1 ){
    $row = mysql_fetch_array( $results );

    echo "while";
    $from = "Name <hmparikh15@gmail.com>";
    $header = "From: ".$from;
    //$header = "Reply-To: ".$from."rn";
    $to = $row['email'];
    $subject = "Password Recovery ";
    $emailBody = "UserName ".$row['UserName']."
    Password ".$row['Password']." \n";
    echo $emailBody;
    echo $to;

}else{
    echo 'User not found!';
}

I reduced your two queries into on join therefore you only have one query to run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.