2014-10-30 52 views
0

没有人可以帮助我,如何在用户注册后发送电子邮件给用户。我是codeigniter的新手。我希望在注册表格提交后立即发送电子邮件。发送电子邮件给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'); 
     } 
    } 
} 
?> 

感谢名单的任何帮助

回答

2

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

$config['protocol'] = 'smtp'; 
    $config['smtp_host'] = 'mail.******.com'; 
    $config['smtp_timeout'] = '7'; 
    $config['smtp_user'] = '[email protected]'; 
    $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('[email protected]', 'Test'); 
    $CI->email->to('[email protected]'); 
    $CI->email->cc('[email protected]'); 
    $CI->email->bcc('[email protected]'); 
    // EXP. $CI->email->bcc('[email protected],[email protected]'); 
    $CI->email->subject('email subject'); 
    $CI->email->message('message body'); 
    $CI->email->send(); 
    //echo $this->email->print_debugger(); 
1

你会使用电子邮件类。 https://ellislab.com/codeigniter/user-guide/libraries/email.html

一旦你的用户在数据库中,你可以给他们发一封电子邮件,像这样;

if ($result) 
{ 
    $this->load->library('email'); 
    $this->email->from('[email protected]', 'Your Name'); 
    $this->email->to($reg_email); 

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

    $this->email->send(); 
} 
+0

thanx for the reply。 但显示错误 消息:mail():无法连接到“本地主机”端口25的邮件服务器,在php.ini中验证您的“SMTP”和“smtp_port”设置或使用ini_set() – hash 2014-10-30 09:44:28

+0

您将拥有设置您的SMTP设置。阅读我发布的用户指南。 – Craig 2014-10-30 09:51:49

0

使用下面的代码将工作..你... 注意:不,如果你是从本地主机发送电子邮件,工作,如果这样..你需要在你的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('[email protected]', '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); 
} 
+0

thanx的答复。但它显示错误消息:邮件():无法连接到邮件服务器在“本地主机”端口25,在php.ini中验证你的“SMTP”和“smtp_port”设置,或者使用ini_set() – hash 2014-10-30 09:55:04

+0

你正在使用哪个服务器,我的意思是XAMPP或任何其他..并让我知道你从哪里测试这个我的意思是从你自己的机器本地主机或从在线服务器本地主机.. – 2014-10-30 09:58:10

+0

先生我使用WAMP服务器 – hash 2014-10-30 09:59:19

相关问题