在codeigniter中向注册用户发送电子邮件

时间:2014-10-30 09:22:33

标签: php codeigniter email

是否有人可以帮助我了解如何在用户注册后立即向用户发送电子邮件。我是codeigniter的新手。我希望在提交注册表后立即发送电子邮件。

这是我的控制器:

<?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');
                        }
                    }
    }
    ?>

这是我的模特:

<?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任何帮助

3 个答案:

答案 0 :(得分:2)

$ CI =&amp; get_instance();         $ CI-&GT;负载&GT;库( '电子邮件');

    $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();

答案 1 :(得分:1)

您将使用电子邮件课程。 https://ellislab.com/codeigniter/user-guide/libraries/email.html

一旦您的用户在数据库中,您就可以向他们发送电子邮件,如下所示;

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();
}

答案 2 :(得分:0)

使用下面的代码将...适合你... 请注意,如果您从localhost发送电子邮件,这将无法工作,如果是这样..您需要在php.ini和sendmail.ini文件中进行设置..

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);
}