Welcome Guest, Not a member yet? Register   Sign In
Multiple Email Not Sending Correct
#1

(This post was last modified: 07-24-2015, 06:48 AM by wolfgang1983.)

I am trying to send multiple email but having small issue. I can get my news letter emails from the database but I can not get the email to send correct. For some reason it only sends the message to the first email in the list and not the others.

How can I make sure it sends to all emails in list.

PHP Code:
public function sendmail() {
    
$config = array( 
        
'protocol' => 'smtp'
        
'smtp_host' => 'ssl://squid.arvixe.com'
        
'smtp_port' => 465
        
'smtp_user' => '******'
        
'smtp_pass' => '******'
    
); 

    
$this->load->library('email'$config);
        
    
$results $this->get_emails();

    foreach (
$results as $result) {

        
$this->email->from(config_item('email'), config_item('website_name'));
        
        
$this->email->to($result['email']);
        
        
$this->email->subject('My mail through codeigniter from localhost'); 
        
        
$this->email->message('Hello World…');

        if (!
$this->email->send()) {
             
            
show_error($this->email->print_debugger()); 
            
        } else {

            
$this->session->set_flashdata('success''Your e-mail has been sent!' $result['email']);
            
redirect('admin/mail');
        }
    }


Email Model


PHP Code:
public function get_emails() {
$this->db->select('email');
$this->db->from($this->db->dbprefix 'news_letter');
$query $this->db->get();

return 
$query->result_array();

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

After you send the first email, you're either going to show_error() or redirect(), so the loop breaks. Instead, you should just track the results and store the debug messages for any failures, then show the errors or success message after the loop. For example:
PHP Code:
public function sendmail() {

    $config = array( 
        'protocol'  => 'smtp'
        'smtp_host' => 'ssl://squid.arvixe.com'
        'smtp_port' => 465
        'smtp_user' => '******'
        'smtp_pass' => '******'
    ); 
    $this->load->library('email'$config);

    $mailResults = array();        
    $results 
$this->get_emails();
    foreach ($results as $result) {
        $this->email->from(config_item('email'), config_item('website_name'));
        $this->email->to($result['email']);
        $this->email->subject('My mail through codeigniter from localhost'); 
        $this->email->message('Hello World…');
        if (! $this->email->send()) {
            $mailResults[] = $this->email->print_debugger();
        }
    }

    if (! empty($mailResults)) {
        show_error(implode('<br />'$mailResults));
    }

    $this->session->set_flashdata('success''Your e-mail has been sent!' $result['email']);
    redirect('admin/mail');

Reply
#3

(07-24-2015, 07:06 AM)mwhitney Wrote: After you send the first email, you're either going to show_error() or redirect(), so the loop breaks. Instead, you should just track the results and store the debug messages for any failures, then show the errors or success message after the loop. For example:

PHP Code:
public function sendmail() {

    $config = array( 
        'protocol'  => 'smtp'
        'smtp_host' => 'ssl://squid.arvixe.com'
        'smtp_port' => 465
        'smtp_user' => '******'
        'smtp_pass' => '******'
    ); 
    $this->load->library('email'$config);

    $mailResults = array();        
    $results 
$this->get_emails();
    foreach ($results as $result) {
        $this->email->from(config_item('email'), config_item('website_name'));
        $this->email->to($result['email']);
        $this->email->subject('My mail through codeigniter from localhost'); 
        $this->email->message('Hello World…');
        if (! $this->email->send()) {
            $mailResults[] = $this->email->print_debugger();
        }
    }

    if (! empty($mailResults)) {
        show_error(implode('<br />'$mailResults));
    }

    $this->session->set_flashdata('success''Your e-mail has been sent!' $result['email']);
    redirect('admin/mail');


Thank you now under understand
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB