Search code examples
codeigniteremailnewsletter

sending email to all newsletter members with codeigniter


I want to send an email using codeigniter library to my newsletter members and I want to mention each members' Email address in email's content. For doing that, I used foreach loop to send the email one by one. The problem is that the code sends email to just one member ( the first member ) of my newsletter. I've checked my code which gets members from the database and it printed out all the members.

This is my model:

function send_news()
{
    $subscribers = $this->get_subscriber_data();
    foreach($subscribers as $subscriber)
    {
        $this->load->helper('typography');

        //Format email content using an HTML file
        $data['news_Title'] = $this->input->post('news_Title');
        $HTML_Message = $this->load->view('admin/includes/newsletter_html_format', $data, true);

        $config['mailtype'] = 'html';
        $this->email->initialize($config);

        $this->email->from('newsletter@site.com', 'newsletter of site.com');
        $this->email->to($subscriber->subscriber_Email);
        $this->email->subject('newsletter of site.com');
        $this->email->message($HTML_Message);

        return $this->email->send();
    }

}

and this is how I'm getting subscribers list:

function get_subscriber_data($options = array())
{
    $query = $this->db->get('mg_newsletter');

    if(isset($options['subscriber_Id']))
        return $query->row(0);

    return $query->result();
}

when I try to echo $subscriber->subscriber_Email it prints all the emails in the database one after another. but it does not send email to all of them. What am I doing wrong?!


Solution

  • You are "return" -ing inside your loop, which exits the function.

    Just send