更新后不发送电子邮件的Grocery Crud回调

时间:2015-02-23 11:25:14

标签: php codeigniter codeigniter-2 grocery-crud

我对CI和GC相当新,所以我的问题可能只是语法问题。我在管理员更新记录后尝试向记录所有者发送电子邮件。发送给管理员的电子邮件有效,但是在更新时发给用户的电子邮件没有任何作用。

控制器中的电子邮件功能:

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

        $this->email->from('myemail@gmail.com', 'Admin');
        $this->email->to('myemail@gmail.com');

        $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('myemail@gmail.com', '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);   
            } 

        }  
    }

正如我所说,可能只是因为我根本不知道我在做什么,但任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分: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>
    ";
}