Using SendGrid with PHP: Part 1 (Codeigniter)SendGrid Team
With the PHP edition of the Battle of the Braces coming up this weekend, I thought that now would be a good time to demonstrate how easy it is to integrate SendGrid with PHP. Specifically, I’ll be diving into three of my personal favorite frameworks – Codeigniter, FuelPHP, and Flourish. In order to keep things manageable, I’ll be dividing the posts into a three part series. This post takes a look at the CodeIgniter framework. By the time you’re done reading this blog post, you’ll be able to set up an application and be sending email in no time!
CodeIgniter was actually the first real MVC framework I ever worked with as a developer. Much to my surprise, it’s still just as popular today as it was when I was starting out (in fact, it may even be more popular these days… go figure!) . Luckily not much has changed during my absence, since setting it up is just as easy today as it was back then!
Step 1: Install Codeigniter
Head over to http://codeigniter.com/ and click on the big “Download” button you see. You’ll get back a .zip file containing the source code for the framework. You should unzip the files and copy them over to the root of your server. Since I was doing this on my local machine, I didn’t have to change any of the default configuration files, but if you’re developing remotely then I would recommend checking out the getting started guide .
Once you have all the files in place and the framework configured, point your browser at your server’s root and you should see something like this:
Step 2: Configure the email library
Now for the fun part! Open up your favorite text editor and create a new file called “email.php” in you “application/config/” directory. All we need to do is configure CodeIgniter to send email via SMTP and then tell it what server, port, username, and password to use. This is what mine looked like:
Step 3: Send an Email
Now that the server is configured, let’s send an email. Typically the logic to send an email (especially transactional email) will reside in the controller since it is triggered by some sort of user interaction. Let’s open up and edit the example Welcome Controller that a fresh Codeigniter install comes with. Here’s a simple example that I’ll be using across this series of tutorials:
As you can see, there’s no magic here. Load up the email library, set some properties, and deliver it!
Interested in learning more about integrating Codeigniter and SendGrid? Awesome! Well, step one would be to check out the Codeigniter documentation for the email library. They have a pretty in-depth overview of things like attaching files and text vs. html encoding.
If you want to take full advantage of everything the SendGrid SMTP API has to offer, you’ll definitely have to check out the source code for the library. There are several undocumented functions that give you much finer grained control over the email (Ex. Headers – which you need for the SMTP API). I’ll have to cover those in a future blog post though! Stay tuned for the rest of the PHP series of posts and more Codeigniter in the future!
NOTE: I took the liberty of creating a sample repository which contains the code I’ll be composing below. Feel free to clone it and use as you see fit!