代码点火器邮件功能
code igniter email function
我有一个小问题,我有一个用 code igniter 编写的完美运行的函数,用于发送电子邮件。但我想知道的是,如果电子邮件发送功能在发送电子邮件时出现问题,我如何才能确保控制器中的其他功能仍在 运行ning。准确的说,这是我的控制器。
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();
从email功能,然后就没有发邮件了,所以我的controller CreateMoney怎么可能在email功能失效的情况下还运行其他一些代码呢。我通过将 return false 放在电子邮件功能的底部来尝试它,但是当我故意在电子邮件功能中出错时,整个控制器功能仍然停止。所以需要一种方法来确保控制器功能保持 运行ning 即使电子邮件功能出现问题
这正是 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);
}
这样,电子邮件发送可能会失败,但控制器仍会运行。
我有一个小问题,我有一个用 code igniter 编写的完美运行的函数,用于发送电子邮件。但我想知道的是,如果电子邮件发送功能在发送电子邮件时出现问题,我如何才能确保控制器中的其他功能仍在 运行ning。准确的说,这是我的控制器。
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();
从email功能,然后就没有发邮件了,所以我的controller CreateMoney怎么可能在email功能失效的情况下还运行其他一些代码呢。我通过将 return false 放在电子邮件功能的底部来尝试它,但是当我故意在电子邮件功能中出错时,整个控制器功能仍然停止。所以需要一种方法来确保控制器功能保持 运行ning 即使电子邮件功能出现问题
这正是 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);
}
这样,电子邮件发送可能会失败,但控制器仍会运行。