0

I am trying to work on forgot password. So if a user forgets his password, he can retrieve it by going to the forgot password page. although everything seems to work perfect, i do get success message "Please check your email address." and validation also works perfect. The database table also regenerated a password in md5 and also a rs = random-string is generated . But i don't get an email , i have tried different emails hotmail,gmail for receiving. And for sending i used only gmail but i did try 2 different gmail accounts. I also faced a weird problem that is, if i use account1@gmail.com the page just loads forever but when i use account2@gmail.com the page successfully loads and shows up the success message. One more thing.. the email is sent when a new user registers ,the code is same for both(copy paste into different function), but it does not work when i am going for forgot password. below are my controller and view files.

controller: mysite

    public function change_password()
      {

      $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_email_check');

       if ($this->form_validation->run() == FALSE)
      {
             $this->load->view('mysite/email_check');
       }
       else
      {

     $email= $this->input->post('email');

$this->load->helper('string');
$rs= random_string('alnum', 12);

$data = array(
               'rs' => $rs
            );
$this->db->where('email', $email);
$this->db->update('user', $data);
$config['protocol'] = 'smtp';

       $config['smtp_host'] = 'ssl://smtp.googlemail.com';

       $config['smtp_port'] = 465;

       $config['smtp_user'] = 'account1@gmail.com';

     $config['smtp_pass'] = 'mypassword';


              $this->load->library('email', $config);

$this->email->from('account1@gmail.com', 'Admin');


$this->email->to($this->input->post('email')); 

$this->email->subject('Get your forgotten Password');
$this->email->message('Please go to this link to get your password.
       http://localhost/final-project/get_password/index/'.$rs );

$this->email->send();
echo "Please check your email address.";
}
      }

the get_password controller is for resetting it..

view file : email_check

<?php echo validation_errors(); ?>
         <form action="<?php site_url('mysite/change_password'); ?>"  method="post">


         <h2> email</h2>
           <input type="text" size="30" id="email" name="email"/>

     <br/>
           <input type="submit"name="submit"  value="Submit"/>
         </form>
Babar Ali
  • 181
  • 1
  • 18

1 Answers1

0

I finally found what was wrong. The code was perfectly fine but still I couldn't figure out why it didn't work.

All i had to add was this line:

$this->email->set_newline("\r\n");

after :

$this->load->library('email', $config);

Now the email is sent. I hope this helps many people too who come up with similar issue.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
Babar Ali
  • 181
  • 1
  • 18