【问题标题】:How can I send multiple email using a loop in CodeIgniter?如何使用 CodeIgniter 中的循环发送多封电子邮件?
【发布时间】:2016-09-02 01:37:47
【问题描述】:

我的代码有问题。我想发送来自不同客户的 10 封电子邮件。我把我的过程放在一个循环中。每个循环我都会将状态列更新为1。我的问题是只能发送 1 封电子邮件。我的循环不会进入下一个电子邮件地址。这是我的代码:

$dummy = $this->Users_model->get_dummy_email(); //produces 10 results.

$valid_email = array();

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

$config = array(
    'protocol'  =>  'sendmail',
    'smtp_host' =>  'mail.sample.ph',
    'smtp_port' =>  587,
    'mailtype'  =>  'html', 
    'charset'   =>  'utf-8'
);

$this->email->initialize($config);
$this->email->set_mailtype("html");

foreach($dummy as $d) { //loop is ok

    $this->email->clear();

    $this->email->from('info@sample.ph', 'Admin');
    $this->email->to($d['email']);
    $this->email->cc('user@sample.ph, mymail@gmail.com');
    $this->email->subject("TEST MESSAGE ONLY USING CRON");
    $this->email->message("TEST FROM ADMIN");

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

    if($send_mail) {

        $this->Users_model->updateDummyStatus($d['id']);
        return true;

    } else {
        echo $this->email->print_debugger();
    }

}

你能帮我解决我的错误吗?我也试过把初始化放在循环里面,效果还是一样的。只能发送 1 封电子邮件。

【问题讨论】:

    标签: php codeigniter email


    【解决方案1】:

    您在发送电子邮件后在循环中使用 return。 return 语句立即结束当前函数的执行。因此,您的剩余电子邮件将不会发送。

    if($send_mail) {
    
        $this->Users_model->updateDummyStatus($d['id']);
        //return true;   <--- Remove this line..
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多