How To Send Email To Multiple Recipients With Attachments Using Codeigniter ?

Sending email is very simple in Codeigniter. The following piece of code will email with attachments to all recipients at once. It will prevent showing all recipients address in email, and will email it separately for each recipient.
//loading necessary helper classes
$this->load->library('email');
$this->load->helper('path');
$this->load->helper('directory'); 

//setting path to attach files 
$path = set_realpath('assets/your_folder/');
$file_names = directory_map($path);

foreach ($list as $name => $address)
{
    //if you set the parameter to TRUE any attachments will be cleared as well  
    $this->email->clear(TRUE);
    $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.'); 
    
    foreach($file_names as $file_name)
    {
     
      $this->email->attach($path.$file_name);
    
    }

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


Here $list contains array of Recipient name and email ID. Make sure to use clear(TRUE) at the beginning of each iteration.
Example used above is taken from http://ellislab.com/codeigniter/user-guide/libraries/email.html.

Comments

Popular Posts