0

I want to send a confirmation mail to users given email id aftre his/her registration. I am trying this in codeIgniter framework only. But I didn't get the exact output. Please help me, what is the correct code I can use here?
Please not: I am using Xaamp server.
Thanks in advance..,

Edited by sandeepek: spelling mistake

3
Contributors
5
Replies
33
Views
4 Years
Discussion Span
Last Post by sandeepek
0

Hi masterjiraya,
Thank you for your replay.
I am giving the codes below..,please check and let me know. And one more thing here I need to configure anything in php.ini file? If it is needed I don't know where I have to change in Xaamp.

**This is my users_model.php (model)**

<?php


     **This is my users_model.php (model)**

    <?php
        class Users_model extends CI_Model {

                public function __construct()
                {
                        $this->load->database();
                }
                    public function get_users($slug = FALSE)
                    {
                            if ($slug === FALSE)
                            {
                                    $query = $this->db->get('users');
                                    return $query->result_array();
                            }

                            $query = $this->db->get_where('users', array('slug' => $slug));
                            return $query->row_array();
                    }
     public function set_users()
                    {
                            $this->load->helper('url');

                            $slug = url_title($this->input->post('username'), 'dash', TRUE);
                            $email_address=$this->input->post('email'); 
                            $data = array(
                                    'username' => $this->input->post('username'),
                                    'slug' => $slug,
                                    'email_address' => $this->input->post('email'),
                                    'password' => $this->input->post('password'),
                                    'image' => $this->input->post('register_image'),
                                    'gender' => $this->input->post('gender')
                            );

                            $user_name_check = $this->db->get_where('users', array('username' => $slug));
                            $email_address_check = $this->db->get_where('users', array('email_address' => $email_address));
                   if($user_name_check->num_rows()>0)
                   {
                           echo '<script>alert("User name already exist");</script>';
                           return "error";
                   }
                   if($email_address_check->num_rows()>0)
                   {
                           echo '<script>alert("Email address already exist");</script>';
                           return "error";
                   }
                            else
                            {
                                $this->db->insert('users', $data);
                                // Registration succees 
                                return $slug;
                            }

                   }
     }
     ?>
     ---------------------------------------------------------------------------------
    ** This is my users.php (controller)**

    <?php
        class Users extends CI_Controller {

        public function registration()
                {
                        $this->load->helper('form');
                        $this->load->library('form_validation');

                        $data['title'] = 'Create a news item';

                         $this->load->helper('form');

        $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '1000';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
        $this->load->library('upload', $config);
        if ($this->users->registration())
            {
                $data = array('upload_data' => $this->users->data());

                $this->load->view('upload_success', $data);
            }  
        $query= $this->users_model->get_user();
                    $rs=$this->users_model->set_users();    

                    if($rs!="error")
                           {
                               # Send confirmation mail with call back url 
                               $secret_flag =  base64_encode($rs);
                               $to="example@gmail.com";
                               $subject="Confirm Registration";
                               $body="<b>Dear ,</b> Please click here to confirm your registration . <br>
                                        ".base_url()."index.php/authentication/confirm?id=".$secret_flag;
                               $this->send_mail($to,$subject,$body);
                               $to="example@gmail.com";
                               $this->send_mail($to,$subject,$body);
                               echo " Please check your mail ..";
                               /// Load login form 
                               //$this->load->view('users/login');
                           }
                    if($rs="error")
                           {
                                $this->load->view('templates/header', $data);
                                $this->load->view('pages/home',$data);
                                $data['msg']='Username and Email address already exist...!';
                                $this->load->view('templates/footer');  
                            } 

           /****************************************
           * Function for sending mail

           *******************************************/
           public function send_mail($to,$subject,$body)
           {
               $this->load->library('email');
               $config['protocol']    = 'smtp';
               $config['smtp_host']    = 'ssl://smtp.gmail.com';
               $config['smtp_port']    = '465';
               $config['smtp_timeout'] = '7';
               $config['smtp_user']    = 'example@gmail.com';
               $config['smtp_pass']    = 'pass';
               $config['charset']    = 'utf-8';
               $config['newline']    = "\r\n";
               $config['mailtype'] = 'html'; // or html
               $config['validation'] = TRUE; // bool whether to validate email or not      

               $this->email->initialize($config);

               $this->email->from('example@gmail.com', 'Sample');
               $this->email->to($to); 
               //$this->email->cc('another@another-example.com'); 
               //$this->email->bcc('them@their-example.com'); 

               $this->email->subject($subject);
               $this->email->message($body);

               $this->email->send();

              // echo $this->email->print_debugger();
           }

           public function confirm()
           {
               $flag='c2luZGh1';
               $decode=base64_decode($flag);
           }
      }
    ?>



            public function __construct()
            {
                    $this->load->database();
            }
                public function get_users($slug = FALSE)
                {
                        if ($slug === FALSE)
                        {
                                $query = $this->db->get('users');
                                return $query->result_array();
                        }

                        $query = $this->db->get_where('users', array('slug' => $slug));
                        return $query->row_array();
                }
 public function set_users()
                {
                        $this->load->helper('url');

                        $slug = url_title($this->input->post('username'), 'dash', TRUE);
                        $email_address=$this->input->post('email'); 
                        $data = array(
                                'username' => $this->input->post('username'),
                                'slug' => $slug,
                                'email_address' => $this->input->post('email'),
                                'password' => $this->input->post('password'),
                                'image' => $this->input->post('register_image'),
                                'gender' => $this->input->post('gender')
                        );

                        $user_name_check = $this->db->get_where('users', array('username' => $slug));
                        $email_address_check = $this->db->get_where('users', array('email_address' => $email_address));
               if($user_name_check->num_rows()>0)
               {
                       echo '<script>alert("User name already exist");</script>';
                       return "error";
               }
               if($email_address_check->num_rows()>0)
               {
                       echo '<script>alert("Email address already exist");</script>';
                       return "error";
               }
                        else
                        {
                            $this->db->insert('users', $data);
                            // Registration succees 
                            return $slug;
                        }

               }
 }
 ?>
 ---------------------------------------------------------------------------------
** This is my users.php (controller)**

<?php
    class Users extends CI_Controller {

    public function registration()
            {
                    $this->load->helper('form');
                    $this->load->library('form_validation');

                    $data['title'] = 'Create a news item';

                     $this->load->helper('form');

    $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '1000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
    $this->load->library('upload', $config);
    if ($this->users->registration())
        {
            $data = array('upload_data' => $this->users->data());

            $this->load->view('upload_success', $data);
        }  
    $query= $this->users_model->get_user();
                $rs=$this->users_model->set_users();    

                if($rs!="error")
                       {
                           # Send confirmation mail with call back url 
                           $secret_flag =  base64_encode($rs);
                           $to="example@gmail.com";
                           $subject="Confirm Registration";
                           $body="<b>Dear ,</b> Please click here to confirm your registration . <br>
                                    ".base_url()."index.php/authentication/confirm?id=".$secret_flag;
                           $this->send_mail($to,$subject,$body);
                           $to="example@gmail.com";
                           $this->send_mail($to,$subject,$body);
                           echo " Please check your mail ..";
                           /// Load login form 
                           //$this->load->view('users/login');
                       }
                if($rs="error")
                       {
                            $this->load->view('templates/header', $data);
                            $this->load->view('pages/home',$data);
                            $data['msg']='Username and Email address already exist...!';
                            $this->load->view('templates/footer');  
                        } 

       /****************************************
       * Function for sending mail

       *******************************************/
       public function send_mail($to,$subject,$body)
       {
           $this->load->library('email');
           $config['protocol']    = 'smtp';
           $config['smtp_host']    = 'ssl://smtp.gmail.com';
           $config['smtp_port']    = '465';
           $config['smtp_timeout'] = '7';
           $config['smtp_user']    = 'example@gmail.com';
           $config['smtp_pass']    = 'pass';
           $config['charset']    = 'utf-8';
           $config['newline']    = "\r\n";
           $config['mailtype'] = 'html'; // or html
           $config['validation'] = TRUE; // bool whether to validate email or not      

           $this->email->initialize($config);

           $this->email->from('example@gmail.com', 'Sample');
           $this->email->to($to); 
           //$this->email->cc('another@another-example.com'); 
           //$this->email->bcc('them@their-example.com'); 

           $this->email->subject($subject);
           $this->email->message($body);

           $this->email->send();

          // echo $this->email->print_debugger();
       }

       public function confirm()
       {
           $flag='c2luZGh1';
           $decode=base64_decode($flag);
       }
  }
?>

Edited by pritaeas: Remove username and password. Be a little more careful.

0

@sandeepek a suggestion: change the password of your gmail account, since it's included in the above script.

0

Hi masterjiraya,
It's working now.., here I found problem in image uploading. I have removed that code , then it is working fine. :-)
Thank you,

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.