Pregunta

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?!

¿Fue útil?

Solución 2

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

Just send

Otros consejos

use

 $this->email->clear()

As codeignitor says: Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles.

foreach ($list as $name => $address)
{
    $this->email->clear();

    $this->email->to($address);
    $this->email->from('your@example.com');
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi '.$name.' Here is the info you requested.');
    $this->email->send();
}

If you set the parameter to TRUE any attachments will be cleared as well. Then let us know

You can keep the loading helper file and email configuration outside the loop and then remove the return from for loop, it will send to all subscribers one by one and then after loop finishes you can do the return statement.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top