2015-02-23 34 views
1

我对CI和GC相当陌生,所以我的问题可能只是语法问题。我试图在管理员更新记录后发送电子邮件给记录的所有者。关于插入的电子邮件给管理员,但是在更新时给用户的电子邮件什么也不做。更新后的Grocery Crud回调不发送电子邮件

电子邮件功能控制器:

public function email_admin_vehicle() 
    { 
     $this->load->library('email'); 

     $this->email->from('[email protected]', 'Admin'); 
     $this->email->to('[email protected]'); 

     $this->email->subject('New Vehicle Added'); 
     $this->email->message('A new vehicle has been registered. Please log in to your admin panel to approve the vehicle.'); 

     $this->email->send(); 

     echo $this->email->print_debugger(); 
    } 
    public function email_user_vehicle($post_array) 
    { 
     $this->load->library('email'); 

     $this->email->from('[email protected]', 'Cars in the Park'); 
     $this->email->to($post_array['email']); 

     $this->email->subject('Vehicle Updated'); 
     $this->email->message('An administrator has updated your vehicle details. Please log on to your account to view the changes.'); 

     $this->email->send(); 

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

回调和CG功能:

//==================\\ 
    //START: Vehicle Crud 
    //==================\\ 
    public function vehicle() 
    { 
     if (!$this->ion_auth->logged_in()) 
     { 
      redirect('auth/login'); 
     } 
     else { 
      if (!$this->ion_auth->is_admin()) { 
     // Get User_id if standard user 
     $user = $this->ion_auth->user()->row(); 
     // Build Crud 
     $crud = new grocery_CRUD(); 
      $crud->unset_jquery(); 
      // Timestamps 
      $crud->set_model('MY_grocery_Model'); 
      // Table 
      $crud->set_table('vehicle'); 
      // Special Queries 
      $crud->where('vehicle.user_id', $user->id); 
      // Display Rules 
      $crud->display_as('registration','Licence Plate'); 
      $crud->display_as('user_id','Name'); 
      $crud->display_as('event_id','Event'); 
      $crud->display_as('stand_id','Stand'); 
      $crud->display_as('club_id','Club'); 
      // Relations 
      $crud->set_relation('event_id','event','event_name'); 
      $crud->set_relation('user_id','users','username'); 
      $crud->set_relation('club_id','club','club_name'); 
      $crud->set_relation('stand_id','stand','code'); 
      // Validation 
      $crud->set_rules('year','Year','integer'); 
      $crud->required_fields('make','model','year','country','registration','event','club'); 
      // Fields Rules 
      $crud->unset_add_fields('created','updated','stand_id','verified','paid','user_id','ref_number'); 
      $crud->unset_edit_fields('created','updated','stand_id','verified','paid','user_id','ref_number'); 
      $crud->unset_columns('user_id','created','updated','verified','paid'); 
      $crud->unset_export(); 
      $crud->unset_print(); 
      // Adding Buttons 
      $crud->add_action('Print', '', 'print/single','ui-icon-print'); 
      // Callbacks 
      $crud->callback_after_update(array($this, 'email_user_vehicle')); 
      // Render 
      $output = $crud->render(); 
      $this->_cruds_output($output); 


     } 
     else { 
       $crud = new grocery_CRUD(); 
       $crud->unset_jquery(); 
       // Timestamps 
       $crud->set_model('MY_grocery_Model'); 
       // Table 
       $crud->set_table('vehicle'); 
       // Special Queries 
       // Display Rules 
       $crud->display_as('registration','Licence Plate'); 
       $crud->display_as('user_id','Name'); 
       $crud->display_as('event_id','Event'); 
       $crud->display_as('stand_id','Stand'); 
       $crud->display_as('club_id','Club'); 
       // Relations 
       $crud->set_relation('event_id','event','event_name'); 
       $crud->set_relation('user_id','users','username'); 
       $crud->set_relation('club_id','club','club_name'); 
       $crud->set_relation('stand_id','stand','code'); 
       // Adding Buttons 


       $crud->add_action('Print', base_url().'/assets/img/print.png', 'printforms/single',''); 
       // Unset Fields 
       $crud->unset_add_fields('created','updated','user_id','ref_number'); 
       $crud->unset_edit_fields('created','updated','user_id','ref_number'); 
       // Callbacks 
       //$crud->callback_after_insert(array($this, 'add_new_club_user')); 
       $crud->callback_after_insert(array($this, 'email_admin_vehicle')); 
       //$crud->callback_after_update(array($this, 'email_user_vehicle')); 


       // Render 
       $output = $crud->render(); 
       $this->_cruds_output($output); 
      } 

     } 
    } 

正如我说,这可能只是我不知道我在做什么可言,但任何帮助,将不胜感激。

+0

'$ crud-> callback_after_update(阵列($此, 'email_user_vehicle'));'和'$ crud-> callback_after_insert(阵列($此,“email_admin_vehicle '));'在哪里交换。该代码与交换的2条语句一样工作。 – 2015-02-23 13:25:48

回答

0

通常如果你使用的是Linux,它会有一个内置的邮件服务器,但就我而言,我在Windows上使用Wamp,它没有内置的邮件服务器。因此我使用phpmailer。

我没有看到您设置SMTP主机和端口。 我使用CI但不使用其电子邮件库,但我发现它比我认为使用本机PHP更简单。

我的代码看起来是这样的:

public function send_confirmation($grocery_no) 
{ 
    require_once(APPPATH.'libraries/class.phpmailer.php'); 
    require_once(APPPATH.'libraries/class.smtp.php'); 


    $order = $this->Grocery_model($grocery_no); 

    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->CharSet="UTF-8"; 
    $mail->SMTPSecure = 'tls'; 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->Port = 587; 
    $mail->Username = '/*your_email_here*/@gmail.com'; 
    $mail->Password = '/*your_password_here*/'; 
    $mail->SMTPAuth = true; 

    $mail->From = '/*your_email_here*/'; 
    $mail->FromName = '/*your_name_here*/'; 
    $mail->AddAddress($order['contact_email']); 

    $mail->AddReplyTo('/*your_email_here*/', 'Admin'); 

    $mail->IsHTML(true); 
    $mail->Subject  = "Your Grocery is Confirmed"; 
    $mail->AltBody  = "To view the message, please use an HTML compatible email viewer!"; 
    $mail->Body   = " 
     Hello, 
    <br> 
    <br>Thank you for shopping with us. 
    <br>Below is your grocery list. 
    <br>Have a nice day. 
    <br> 
    <br> 
    <br>Regards, 
    <br> 
    <br> 
    "; 
}