Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send smtp mail in codeigniter?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 441
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss how to send SMTP mail in codeigniter?

    In a web applications sending email is a very common feature.This is used for sending Emails messages,notifications and newsletters.
    If you are making a web application in codeigniter framework then you need to create a smtp mail for sending mail to reciepant,because Email is the important functionality in web applications.

    You can use send email when any user has completed his registration form or any user forgot his password then you can send new password on his personal email just after fill some requirment field like his email account.

    In CodeIgniter sending Email  is very Simple process just configuration of Email can be done in one of the Controller file of our Application.

    You can see in below example there is a forgot_password function in which we are sending a smtp mail to user with 9 random number on his emailid and also update that password in database.

    You have to also set a email account and password for sending email to user.Because without sender a receiver can not receive a email.

    
    <?php
    /* forgpt password function */ 
    
        public function forgot_password($username)
        {
          $this->db->select('id,email');
          $this->db->from('user_registration');
          $this->db->where('username',$username);
          $result= $this->db->get();
          $result->result_array();
    
          if($result->num_rows()==1)
          {
            //create a random password of 9 digits
            $pass=rand (99999999 ,999999999);
            //convert in to MD5 password 
            $data1['password'] = MD5($pass);
    
            $id =$result->result_array[0]['id'];
            $email =$result->result_array[0]['email'];
    
            //here update tghe password in database
            $this->db->where('id', $id);
            $this->db->update('user_registration', $data1);
    
             /*send mail function*/
              $mail_message='Dear User your new password is '.$pass.',<br><br><br>';
              $mail_message.='Thanks & Regards<br>Admin';
              $this->load->library('email');
              $config['protocol'] = "smtp";
              $config['smtp_host'] = "smtpout.secureserver.net";
              $config['smtp_port'] = "25";
              $config['smtp_user'] = "example@gmail.com";
              $config['smtp_pass'] = "123456";
              $config['wordwrap'] = TRUE;
              $config['mailtype'] = 'html';
              $this->email->initialize($config);
              $this->email->from('admin@info.com','admin');
              $this->email->to($email);
              $this->email->subject('Password changed and email sent.');
              $this->email->message($mail_message);
              $this->email->send();
             /*end send mail function*/
    
              //set massege after send mail into user
              $data['message']="message sent successfullt";
              $data['status']="success";
              return $data;
    
          }else{
              $data['message']="message not sent";
              $data['status']="Fail";
              return $data;
            }
    
    
       }
    ?>
    

    I hope this blog is helpful for sending an email in Codeigniter.

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: