0

does anyone can help me out that how to send an email to user right after the user is registered. i am newbie to codeigniter. i want to send an email right after the register form is submitted.

this is my controller :

<?php

    class Registration extends CI_Controller {

        public function index()
        {
            parent::__construct();
            $this->load->view('registration_view');
        }
    public function userReg() {

            if(extract($_POST)){
                //Validation of login form
                $this->form_validation->set_rules('reg_fname', 'First Name', 'required');
                $this->form_validation->set_rules('reg_lname', 'Last Name', 'required');
                $this->form_validation->set_rules('reg_email', 'Email', 'required|valid_email|is_unique[fw_registeration.reg_email]');
                $this->form_validation->set_rules('reg_pass', 'Password', 'required|matches[reg_conpass]|min_length[6]|max_length[12]');
                $this->form_validation->set_rules('reg_conpass', 'ConfirmPassword', 'required');


                if($this->form_validation->run() == FALSE){
                    $data['reg_fname'] = $reg_fname;
                    $data['reg_lname'] = $reg_lname;
                    $data['reg_email'] = $reg_email;
                    $data['reg_pass'] = $reg_pass;
                    $data['reg_conpass'] = $reg_conpass;

                    $this->load->view('registration_view', $data);  
                }else{
                    //checking username and password from db
                    $this->load->model('reg_model');
                    //$login_status = $this->form_model->userLogin($user_login_name, $user_login_password);
                    //registeration status
                    $reg_status = $this->reg_model->userReg($reg_fname, $reg_lname, $reg_email, $reg_pass, $reg_conpass);
                    if($reg_status == true){

                        $data['reg_status'] = "Welcome, you logged in.";    
                    //  $this->load->view('login', $data);
                    }else{
                        $data['reg_status'] = "Login Failed. Please Try Again...";  
                        //$this->load->view('loginsuccess', $data);
                    }
                }
            }
                        else{
                $this->load->view('registration_view');
                        }
                    }
    }
    ?>

this is my model :

<?php
class Reg_model extends CI_Model {
    public function userReg($reg_fname, $reg_lname, $reg_email, $reg_pass, $reg_conpass) {
        $sql = "INSERT INTO fw_registeration (reg_fname, reg_lname, reg_email, reg_pass, reg_conpass) VALUES (".$this->db->escape($reg_fname).", ".$this->db->escape($reg_lname).", ".$this->db->escape($reg_email).", ".$this->db->escape($reg_pass).", ".$this->db->escape($reg_conpass).")";

        //echo $this->db->affected_rows();
        if ($result = $this->db->query($sql, array($reg_fname, $reg_lname, $reg_email, $reg_pass, $reg_conpass)));
        {
            //echo base_url();
            // $this->load->view('home');
            $this->load->view('index_new');
        }
    }
}
?>

Thanx for any help

tereško
  • 58,060
  • 25
  • 98
  • 150
H45H
  • 1,019
  • 10
  • 28

3 Answers3

2

$CI =& get_instance(); $CI->load->library('email');

    $config['protocol']    = 'smtp';
    $config['smtp_host']    = 'mail.******.com';
    $config['smtp_timeout'] = '7';
    $config['smtp_user']    = 'abctest@test.in';
    $config['smtp_pass']    = '*******';
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'html'; // or html
    $config['validation'] = TRUE; // bool whether to validate email or not

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

    $CI->email->from('test.test@test.in', 'Test');
    $CI->email->to('abctest123@test.in');
    $CI->email->cc('rec_ccc@test.com');
    $CI->email->bcc('rec_bcc@test.com');
    // EXP. $CI->email->bcc('t111@test.in,ttt2222@test.in');
    $CI->email->subject('email subject');
    $CI->email->message('message body');
    $CI->email->send();
    //echo $this->email->print_debugger();
bhuvnesh
  • 44
  • 3
1

You would use the Email class. https://ellislab.com/codeigniter/user-guide/libraries/email.html

Once your user is in the database, you can send them an email, like this;

if ( $result )
{
    $this->load->library('email');
    $this->email->from('your@example.com', 'Your Name');
    $this->email->to($reg_email); 

    $this->email->subject('Thanks for regstering');
    $this->email->message('Thank you, big lad!');   

    $this->email->send();
}
Craig
  • 1,823
  • 1
  • 11
  • 12
  • thanx for the reply. but its showing an error Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() – H45H Oct 30 '14 at 09:44
  • You will have to set up your SMTP settings. Read the user guide I posted. – Craig Oct 30 '14 at 09:51
  • You could use Google's SMTP settings, whilst you're developing on localhost; https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server – Craig Oct 30 '14 at 09:54
  • sir there is a problem please help me out when i am giving $reg_email to $this->email->to($reg_email); it showing me the smtp error :(. is there any way to send email to the registered e-mail. – H45H Nov 07 '14 at 11:05
0

use the below code will work.. for you... note this will not work if you are sending email from localhost, if so.. you need to do setting in your php.ini and sendmail.ini files..

if($reg_status == true){
    $this->load->library('email');
    $config_email['protocol'] = 'mail';
    $config_email['mailtype'] = 'html';
    $this->email->initialize($config_email);

    $this->email->from('no-reply@yourdomain.com', 'Your Domain');
    $this->email->to($reg_email);
    $this->email->subject('We welcome you');
    $this->email->message("Thanks for registration");

    if ($this->email->send()) {
       $data['reg_status'] = "Welcome, you logged in.";  
    } else {
      $data['reg_status'] = "SOrry registration fail";  
    }

    //$this->load->view('login', $data);
}
Mohammed Sufian
  • 1,743
  • 6
  • 35
  • 62
  • thanx for the reply. but its showing an error Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() – H45H Oct 30 '14 at 09:55
  • which server you are using i mean XAMPP or any other..and let me know from where you are testing this i mean from your own machine localhost or from online server localhost.. – Mohammed Sufian Oct 30 '14 at 09:58
  • Sir i am using WAMP SERVER – H45H Oct 30 '14 at 09:59
  • and using own local machine localhost as well – H45H Oct 30 '14 at 09:59
  • this might help you.. http://stackoverflow.com/a/24162023/2847436 .. note in your own machine at localhost if you are sending email you need to set the mail setting for that but if you are testing it on any online server.. you dont have to do any settings.. – Mohammed Sufian Oct 30 '14 at 10:02