【发布时间】:2016-03-29 16:05:01
【问题描述】:
我有一个小问题,我有一个用代码点火器编写的完美工作功能,用于发送电子邮件。但是我想知道如果电子邮件发送功能在发送电子邮件时出现问题,我该如何确保控制器中的其他功能仍在运行。准确地说,这是我的控制器。
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 放在电子邮件功能的底部进行了尝试,但是当我故意在电子邮件功能中出错时,整个控制器功能都会停止。所以需要一种方法来确保即使电子邮件功能出现问题,控制器功能也能继续运行
【问题讨论】:
-
在 if(!$returnvalue){ $this->email->print_debugger(); 中使用 thisis 函数查看服务器返回的内容。
标签: php codeigniter email codeigniter-2