我已经编写了一个代码,用于向控制器中的多个电子邮件地址发送电子邮件,但我想我写错了,而且我不确定我是否也在forloop的写入部分编写了明确的电子邮件。对不起,这可能是一个简单的问题,但我是这个领域的新人。
public function sendEmailforUnpaidInvoiceFromDB() {
//fetch tha data from the database
$this->load->model('invoice_page');
$foo = $this->invoice_page->getInvoicesForNotification();
$result = json_decode(json_encode($foo[0]), true);
foreach ($foo as $result){
$this->load->library('email');
$this->email->from('helloworld@hello.com', 'HELLO');
$this->email->to($result['Email']);
$this->email->subject('Pay Payments');
//$msg = $this->load->view('mypayment_view', '', true);
$this->email->message('your due date is '.$result['duedate']);
$returnvalue = $this->email->send();
if(!$returnvalue) {
alert("email failed to send");
} else {
// $uptArray = array('customer_notified_dt' => NOW());
//$this -> db -> update('invoice', $uptArray);
}
$this->email->clear(TRUE);
}
}
答案 0 :(得分:2)
您正在尝试将对象作为数组读取,就像错误所说的那样。尝试替换这些:
$this->email->to($result['Email']);
$this->email->message('your due date is '.$result['duedate'])
有了这些:
$this->email->to($result->Email);
$this->email->message('your due date is '.$result->duedate);
答案 1 :(得分:2)
问题在这里,
$result = json_decode(json_encode($foo[0]), true);
foreach ($foo as $result){
^// overwriting previous variable
如果$result
是对象,请使用->
$result->email;
如果是数组
$result['email'];
修改强>
你只得到一行,因为,
$result = json_decode(json_encode($foo[0]), true);
^
您只考虑第一个索引。将0
删除为索引。
答案 2 :(得分:0)
试试这个:
public function sendEmailforUnpaidInvoiceFromDB() {
//fetch tha data from the database
$this->load->model('invoice_page');
$foo = $this->invoice_page->getInvoicesForNotification();
foreach ($foo as $result){
$this->load->library('email');
$this->email->from('helloworld@hello.com', 'HELLO');
$this->email->to($result->Email);
$this->email->subject('Pay Payments');
//$msg = $this->load->view('mypayment_view', '', true);
$this->email->message('your due date is '.$result->duedate);
$returnvalue = $this->email->send();
if(!$returnvalue) {
alert("email failed to send");
} else {
// $uptArray = array('customer_notified_dt' => NOW());
//$this -> db -> update('invoice', $uptArray);
}
$this->email->clear(TRUE);
}
}