Clearing Attachments Using CodeIgniter’s Email Class
The CodeIgniter Email Class makes it very simple to send emails by handling the creation of mail headers and more on your behalf. My favourite benefit of the class however is it’s attachment handling. Whereas using standalone PHP it would take multiple lines of complex code to attach a single file, CodeIgniter allows us to do the same thing by calling just one function like so:
I had a problem recently however where I had attached a file using the above code, sent the email and needed to send another email straight after in the same script. As outlined in the CodeIgniter documentation I used the following code to clear all the variables and begin a fresh email:
This seemed to work to a degree. The attachment however was still coming through on the second email, even after calling the clear() function.
So how do I clear the attachments as well?
After reading further into the documentation I found that there is an optional boolean parameter that can be passed to the email classes clear() function that, if set to TRUE, will clear attachments also. It turns out that by default attachments are not removed. Our code should now look as follows:
Voila! No more attachments on the following emails sent out.