Send mail to user if he forget the password

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Send mail to user if he forget the password

    I'm developing web site using php and my sql. In there i want to send a mail including new password to user, when he forget his password. How can i send mail using php? Could you please help me?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    There is a built in mail() function that is easy to use. Look it up, try the code and post again if you still have problems.

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      Originally posted by r035198x
      There is a built in mail() function that is easy to use. Look it up, try the code and post again if you still have problems.

      Yes. I tried it. But got an error saying "Access denied for user 'root'@'localho st' (using password: NO)". Is there any setting for server? This is my code
      [PHP]
      <?php
      $err=0;

      if(isset($_POST['forgot'])){
      $send = $_POST['forgot'];

      $sql = "SELECT Email FROM user_admin WHERE Email = '".$_POST["email"]."'";
      $rs_email = mysql_query($sq l) or die(mysql_error ());

      if (mysql_num_rows ($rs_email) > 0){
      $send = $_POST['forgot'];
      $err=0;
      }
      else{
      $send="";
      $err=1;
      }
      }

      if ($send == "forgot"){

      $letters = array("A","B"," C","D","E","F", "G","H","I","J" ,"K","L","M","N ",
      "O","P","Q","R" ,"S","T","U","V ","W","X","Y"," Z","0","1"," 2",
      "3","4","5","6" ,"7","8","9","0 ");

      $pass="";

      $passlength=ran d (8, 12);

      for($i=0;$i<$pa sslength;$i++){
      $pass.= $letters[rand (0, 35)];
      }

      $notencrypted=$ pass;
      $pass=md5($pass );

      //echo $notencrypted;

      $sql = "UPDATE user_admin SET Password ='".$pass."' WHERE Email = '".$_POST["email"]."'";


      $body = "";
      $body = $body."<p>Your password is : ".$notencrypted ."</p>";

      $subject = "Password Recovery";
      $toemail = trim($_POST["my@gmail.c om"]);

      $headers = "From: Me <my@yahoo.com>\ n";
      $headers = $headers."Conte nt-Type: text/html; charset=iso-8859-1\n"; // Mime type

      mail($toemail, $subject, $body, $headers);

      echo '<p>Your password has been changed and sent to your email address</p><br />';

      }
      else{
      ?>
      <p>Please provide your email address and your password will be emailed to you</p><br />
      <?php

      if($err==1)
      echo '<p style="color:#9 92928;">The email address that you were entered is not an existing one</p>';
      ?>
      <form name="forgotpas s" id="forgotpass " method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" onsubmit="retur n validateforgot( this);">
      <input name="email" id="email" type="text" size="25" /> <br />
      <input name="forgot" value="forgot" id="forgot" src="images/submit.jpg" type="image" style="margin:5 px 0 0 0;" />
      </form>
      <?php
      }
      ?>
      [/PHP]

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        There is a permission problem or your user name/passwords are incorrect of your MySQL connection String. But how did you connect to your database? I can't see the lines of coding for that.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by ak1dnar
          There is a permission problem or your user name/passwords are incorrect of your MySQL connection String. But how did you connect to your database? I can't see the lines of coding for that.
          Yep, if you call mysql_query() before mysql_connect() , PHP will use default values (specified in php.ini) to try and connect to the database. Those defaults are usually just a username with no password.

          Comment

          • ghjk
            Contributor
            • Jan 2008
            • 250

            #6
            Originally posted by r035198x
            Yep, if you call mysql_query() before mysql_connect() , PHP will use default values (specified in php.ini) to try and connect to the database. Those defaults are usually just a username with no password.
            Connect my sql using [PHP]include 'dbconnect.php' ;[/PHP] .

            Comment

            • ghjk
              Contributor
              • Jan 2008
              • 250

              #7
              Can some one tell me how can i connect to remote mail server and test my code? Is there any way?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by ghjk
                Can some one tell me how can i connect to remote mail server and test my code? Is there any way?
                Perhaps you are not understanding the problem here. That error message simply means that you are not getting access to your mysql database due to incorrect username/password. Try to solve that one first and see what happens.

                Comment

                • ghjk
                  Contributor
                  • Jan 2008
                  • 250

                  #9
                  Originally posted by r035198x
                  Perhaps you are not understanding the problem here. That error message simply means that you are not getting access to your mysql database due to incorrect username/password. Try to solve that one first and see what happens.

                  Understood. I just try using
                  [PHP]<?php
                  $to = "my@gmail.c om";
                  $subject = "Test mail";
                  $message = "Hello! This is a simple email message.";
                  $from = "my@yahoo";
                  $headers = "From: $from";
                  mail($to,$subje ct,$message,$he aders);
                  echo "Mail Sent.";
                  ?>[/PHP]
                  This is working. I put this file to a server which has mail server. But i'm working on my local machine and is there any way to run that file on my machine with connecting mail server in another machine?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by ghjk
                    Understood. I just try using
                    [PHP]<?php
                    $to = "my@gmail.c om";
                    $subject = "Test mail";
                    $message = "Hello! This is a simple email message.";
                    $from = "my@yahoo";
                    $headers = "From: $from";
                    mail($to,$subje ct,$message,$he aders);
                    echo "Mail Sent.";
                    ?>[/PHP]
                    This is working. I put this file to a server which has mail server. But i'm working on my local machine and is there any way to run that file on my machine with connecting mail server in another machine?
                    If the user can access the database from the database machine only then you need to grant them priviledges for remote access as well.

                    Comment

                    • ghjk
                      Contributor
                      • Jan 2008
                      • 250

                      #11
                      Originally posted by r035198x
                      If the user can access the database from the database machine only then you need to grant them priviledges for remote access as well.

                      Still i'm confusing. The server which i hosted my web site doesn't have mail server. But There is another server which is having mail server. So i want to know how to connect that mail server using my website to send mail.

                      Comment

                      Working...