Welcome Guest, Not a member yet? Register   Sign In
email->attach() attaching file twice
#1

[eluser]ImageSmith[/eluser]
Well this is a bit of a doozie. I'm sure it is a simple fix but I'm not seeing it right now :ohh:
I am generating 2 emails as a result of a form submission (validated, etc). One email to the client, one to the site owner / organisation. I have a function for each email type (client, org).
The form result has been written into a PDF using FPDF.
The pdf is attached to both emails. However (this is the odd thing), the pdf is attaching to the org email twice :-S .
I have called the clear method in each function, but the issue persists.
Here are the mail functions:
Code:
function _generate_client_email($message_view,$subject,$attachment = '')
{
  if (!$this->allow_email_send) { // set in config file
    return;
  }
  $ci =& get_instance();
  $ci->load->library('email');
  
  $config['mailtype'] = 'html';
  $ci->email->clear();
  $ci->email->initialize($config);
  
  $ci->email->to($ci->validation->email);
  $ci->email->from($ci->config->item('enquiry_form_recipient'));
  $ci->email->subject($subject);
  $ci->email->message($ci->load->view($message_view,'',true));
  $ci->email->set_alt_message($ci->load->view($message_view.'_plain','',true));
  if ($attachment) {
    $ci->email->attach($attachment);
  }
  $ci->email->send();
}

function _generate_org_email($message_view,$subject = 'Website Email',$params = '',$attachment = '')
{
  if (!$this->allow_email_send) { // set in config file
    return;
  }
  $ci =& get_instance();
  $ci->load->library('email');
  
  $params['from_name'] = $params['from_name'] ? $params['from_name'] : $ci->validation->first_name.' '.$ci->validation->last_name;
  
  $config['mailtype'] = 'html';
  $ci->email->clear();
  $ci->email->initialize($config);
  
  $ci->email->from($ci->validation->email,$params['from_name']);
  $ci->email->to($ci->config->item('enquiry_form_recipient'));
  $ci->email->subject($subject);
  $ci->email->message($ci->load->view($message_view,$params,true));
  $ci->email->set_alt_message($ci->load->view($message_view.'_plain','',true));

  if ($attachment) {
    $ci->email->attach($attachment);
  }
  $ci->email->send();
}
The client function is called first.

EDIT - if I call the org function first then I get 2 attachments in the client email.
Is there something that I should be clearing further here?
#2

[eluser]joeles[/eluser]
Try $ci->email->clear(true). The parameter tells it to clear attachments as well.
#3

[eluser]ImageSmith[/eluser]
Joeles, I just made the change as per your suggestion, uploaded to test server and submitted the form. OMG! it works :-)
You are THE man!
Now that's a little item that should be included in the docs. Any idea who we ask to get stuff considered for future inclusion in docs?

Thanks again.
#4

[eluser]joeles[/eluser]
Actually, it is in the docs, but don't worry I missed it the first time, too. Wink
#5

[eluser]ImageSmith[/eluser]
By jove, I think you're right.
I've read that page at least a dozen times and never spotted that line. (D'Oh!) Confusedhut:
Cheers again.
#6

[eluser]Chillahan[/eluser]
Wow, hooray for this post! I had the same issue, and I was even MORE confused since I have my e-mailing code in a function that I call within a loop (so it seemed more likely that things should definitely be clear, and that perhaps there was some bug involved in CI). Then I found the clear function but I, too, completely missed that TRUE option! Just seems odd that the send() method wouldn't automatically clear, no? I call this now before sending each time to be safe.




Theme © iAndrew 2016 - Forum software by © MyBB