I’m currently implementing SendGrid for a project I’m working on and one of their features is the ability to add Categories, Scheduling, Sections, etc. by making use of a custom email header named X-SMTPAPI. The project was made on CodeIgniter so of course we’d be making use of its built-in email library. Details on adding custom header information, however, is not readily available on their documentation.

Like what any good Geekysaur would do, I tried Googling for the solution. There’s not much relevant results on the topic (hence this blog post) and when I did find one, it required modifying the core email library. I frown upon modifying core framework files as it becomes a maintenance nightmare later on so I looked for other ways to achieve my goal.

The Solution

The core email class has a protected method named _set_header which was responsible for setting the email headers. We don’t want to modify this or make it public but looking at that function showed us that it’s actually just setting a class variable.

Looking further at the variable, I discovered that it is not private or protected and is accessible from outside the class. So, from your controller, you could just call it as:

There you have it! Did this help you? Have you found other ways of setting the email headers in CodeIgniter? Leave your comments below!