如何在电子邮件Codeigniter中循环收件人

时间:2019-07-02 16:40:17

标签: php codeigniter email

尝试将电子邮件发送给多个收件人,但仅发送了1个数据:

function reminder(){
    $recipients= $this->user_model->view();
    var_dump($recipients[0]->email);
    $emaill = $recipients->email;
    $recipientsmail= $emaill.',';
    $email = $recipientsmail;
    $judul = 'Test Email';
    $deskripsi = 'TESt Email';

    $config = [...]; //config for email is OK

    $this->load->library('email', $config);
    $this->email->from('tes');
    $this->email->to($email); 
    $this->email->subject($judul);
    $this->email->message($deskripsi);
    $this->email->send();
    return TRUE;

}

我的代码有问题吗? 请帮助我

1 个答案:

答案 0 :(得分:1)

这就是我用来在codeigniter中发送多封电子邮件的方式。不用将所有电子邮件方向放在变量($email)中,而是使用foreach来循环数组并遵循代码中的详细信息:

function reminder(){
    $recipients= $this->user_model->view();
    var_dump($recipients[0]->email);

    $judul = 'Test Email';
    $deskripsi = 'This is a test';
    $emailuser = 'user123@gmial.com';//for example
    $nameuser = 'name of the user';

    $config = [...]; //config for email is OK

    $this->load->library("email");

  foreach ($recipients as $value) {
    $this->email->initialize($config);
    $this->email->from($emailuser, $nameuser);
    $this->email->to($value->email); 
    $this->email->subject($judul);
    $this->email->message($deskripsi);
    if($this->email->send()){
        $this->session->set_flashdata("email_sent","Email sent successfully."); 
    }else{
        $this->session->set_flashdata("email_sent","Error in sending Email."); 
    }
  }
  return TRUE;
 }

与此同时,您可以发送多个电子邮件。希望对您有帮助。