代码点火器电子邮件功能

时间:2015-12-23 09:03:03

标签: php codeigniter email codeigniter-2

我有一个小问题,我有一个用代码点火器编写的完美工作函数,用于发送电子邮件。但是我想知道如果电子邮件发送功能在发送电子邮件时遇到一些问题,我怎么能确保控制器中的其他功能仍在运行。确切地说,这是我的控制者。

public function CreateMoney($token){        
        if ($this->input->is_ajax_request()){
            $alldata = json_decode(file_get_contents('php://input'), true);
            $userid = $this->myajax->getUserByAuth($token);
            if ($userid){

                /* some code here */


                $this->db->trans_commit();

                $this->sendEmail();

               /* some more code here */

                $this->output->set_content_type('application/json');
                $this->output->set_output(json_encode($invoice));


    } else {
                return $this->output->set_status_header('401', 'Could no identify user!');
    }
        } else {
            return $this->output->set_status_header('400', 'Request not understood as an Ajax request!');
        }    
}

因此,当我单击一个按钮时,它会运行此控制器代码,然后当它转到电子邮件功能时,它会运行此代码,

public function sendEmail() {
    //fetch tha data from the database 
        $this->load->model('payment_plan');
        $foo = $this->payment_plan->getInfoToNotifyBorrowerForNewLoan();
        $result = json_decode(json_encode($foo[0]), true);

        $this->load->library('email');
        $config['protocol'] = 'smtp';

        $config['smtp_host'] = 'ssl://smtp.gmail.com';

        $config['smtp_port']    = '465';

        $config['smtp_timeout'] = '7';

        $config['smtp_user']    = '*******';

        $config['smtp_pass']    = '*******';

        $config['charset']    = 'utf-8';

        $config['newline']    = "\r\n";

        $config['mailtype'] = 'text'; // or html

        $config['validation'] = TRUE; // bool whether to validate email or not      

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

        $this->email->from('*******', '*******');
        $this->email->to($result['Email']);
        $this->email->subject('*******');

        $name               = $result['Fullname'];

        $this->email->message(
            'Hello! '.$name."\n"."\n"



            );  
    $returnvalue = $this->email->send();

    if(!$returnvalue) {
      return false;
    } else {
     $data = array('notified_borrower' => date('Y-m-d'));
     $this->db->update('payment_plan', $data);
     return true;
    }
}

所以我的问题是,如果发送电子邮件时出错,就像我删除

一样
$returnvalue = $this->email->send();

从电子邮件功能,然后没有发送电子邮件,所以即使电子邮件功能失败,我的控制器CreateMoney如何仍然运行其他一些代码。我通过在电子邮件功能的底部放置return false来尝试它,但是当我故意在电子邮件功能中出错时,整个控制器功能都会停止。因此,即使电子邮件功能存在问题,也需要一种确保控制器功能继续运行的方法

1 个答案:

答案 0 :(得分:3)

这正是try/catch块的用途。您会找到更多信息on Exceptions here。这是一些虚拟代码:

CreateMoney()

$log = array();
try {
    $this->sendEmail();
} catch(Exception $e) {
    $log[] = $e->getMessage();
    // just some stupid dummy code
    // do sth. more useful with it!
}

// afterwards
if (count($log) > 0) {
    // some error occured
    // db has not been updated
}

sendEmail()函数

// sth. went wrong here
if (!$this->email->send())
    throw new Exception("Could not send email");
else {
    $data = array('notified_borrower' => date('Y-m-d'));
    $this->db->update('payment_plan', $data);
}

这样,电子邮件发送可能会失败,控制器仍会运行。