Welcome Guest, Not a member yet? Register   Sign In
creating file, writing file and attaching multiple files to an email, only should attach one per email
#1

[eluser]Brad K Morse[/eluser]
I have a function that emails each venue chairman an email with an attached excel file, that is generated using the http://codeigniter.com/wiki/Excel_Plugin/ plugin, it was modified to write the file to the directory "./attachments", and return true if it wrote the file.

I am testing this and in some emails, I'll have 8 attachments, some will have 3 or 4, but each email should contain only one attachment.

I read thru my code and do not know why it attaches multiple files to the email.

The files open properly in excel and display the appropriate data, but each email should contain only one attachment.

Any help is appreciated.

Controller function

Code:
// email lists to each venue
function email_venue() {
  $this->_isUserLoggedIn(); // checks for session
  $this->load->plugin('save_excel_file');  // modified to_excel file
  $this->load->model('admin_model');
  $this->load->model('data_model');
  $data['venues'] = $this->admin_model->getVenues();
  
  // for each venue chairman, grab their id and pull all volunteers assigned to their venue
  foreach($data['venues'] as $r):
    print '<h3>'.$r->email.'</h3>';
    
    $query = $this->admin_model->saveToExcel($r->id); // grab volunteers for that venue
    
    $find = array(' ', '/');
    $replace = array('_', '_');
    $venue_title = $this->data_model->venueOfVolunteer($r->id);
    $venue_title_filename = str_replace($find, $replace, $venue_title); // make appropriate as a filename
    $filename = $venue_title_filename.'-'.date('Ymdhis',time()).'.xls';
    
    $fieldnames = 'First name,Last name,Venue,Email,Phone,Contact,Thursday,Fri,Sat,Sun,Mon,Comments'; // column names in excel file
    
    if(save_excel_file($query, $filename, $fieldnames)) {   // if a file was written in plugin
      $email = "[email protected]";  // using this for test
      $message = "This is the body of the email";
      $this->email->from("[email protected]", "Festival Email");
      $this->email->to($email);
      $this->email->subject('Volunteers for '.$venue_title);  // get venue title for subj line
      $this->email->attach('./attachments/'.$filename);
      $this->email->message($message);    
      $this->email->send();
      print '<p>EMAILED</p>';
    } else {
      print '<p>NOT EMAILED</p>';
    }
  endforeach;
}

Snippet of the code that I replaced the forced download code with, it writes the file to the directory.

save_excel_file_pi.php:
Code:
if ( ! write_file('./attachments/'.$filename, $data)) {
  return FALSE;
} else {
  return TRUE;
}

Attached is an image of the attachment portion of one of the emails, it might not be helpful, but I figured I'd include it.

Let me know if you need more info.

RESOLVED: added
Code:
$this->email->clear(TRUE);
to the beginning of the foreach loop.
#2

[eluser]InsiteFX[/eluser]
I think it is because you have this code in the foreach loop
Code:
if(save_excel_file($query, $filename, $fieldnames)) {   // if a file was written in plugin
      $email = "[email protected]";  // using this for test
      $message = "This is the body of the email";
      $this->email->from("[email protected]", "Festival Email");
      $this->email->to($email);
      $this->email->subject('Volunteers for '.$venue_title);  // get venue title for subj line
      $this->email->attach('./attachments/'.$filename);
      $this->email->message($message);    
      $this->email->send();
I would move the email code out of the foreach for building the excel.
Then use a foreach to email the attachments.

In other words I would break it down more to have a method to create the ecel attachments and create another method to send the emails.

InsiteFX
#3

[eluser]Brad K Morse[/eluser]
Thanks! Put the email code into a private method.




Theme © iAndrew 2016 - Forum software by © MyBB