Integrating Sendgrid with CodeIgniter


Thankyou tweet from Sendgrid :)

As promised in my previous post, here are the steps to set up email on CI using SendGrid

For simple requirements like sending notification mails to the site-admin ( like “A new user just signed up on your website”  or “Error on Production – …” ), the default email class which comes in with CodeIgniter is sufficient. As long as the site-users are not receiving the email, we can use this.

However, if the users will also be receiving emails, (Account confirmation email, password reset requests, notifications from the application etc.) one must use a service like SendGrid (Cloud based SMTP infrastructure) and integrate it to work with CodeIgniter. It ensures that your emails are not delivered to spam folders, offers analytics and lot of other nice stuff.

First, you will need an account on SendGrid –

You will need your SendGrid username and password. Once you have them, edit the following files to integrate it with your codebase –


This file should look like –

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

$config[‘mailtype’] = ‘html’;
$config[‘charset’] = ‘utf-8’;
$config[‘protocol’] = ‘smtp’;
$config[‘smtp_host’] = ‘’;
$config[‘smtp_user’] = ‘your_sendgrid_username_here’;
$config[‘smtp_pass’] = ‘your_sendgrid_password_here’;
$config[‘smtp_port’] = 587;
$config[‘crlf’] = “\r\n”;
$config[‘newline’] = “\r\n”;

/* End of file email.php */
/* Location: ./application/config/email.php */

Now we will create a library, so that we can write the code for sending emails just once and reuse it in all our controllers where we need to send emails –

Create /application/libraries/Email_Assistant.php

(Note : Follow the CI naming convention while naming your class and filename, else you will not be able to load it in your controller)

class Email_Assistant{

function __construct(){

$this->ci =& get_instance();


function send_email($email, $subject, $message){

$this->ci->email->from(‘’, ‘Your App Name’);




///////////////// Specific Functions /////////////////

function send_invitee_message_after_requesting_invite($email){

$subject = “Your invitation confirmation”;
$message = “Thanks for requesting an invite!”;



Inside your controller where you wish to send the emails –



$email = “”;

$email_assistant = new Email_Assistant();

Thats it !!!

In case you have never used SendGrid before, let me assure you that it is quite reliable :) I have used it in a lot of projects and it never gave me any trouble. The admin interface it provides is also neat and you can customize a lot of stuff over there too !

Under the hood –

If you wish to explore more on how the Email class is implemented, you can check out these files –

CI Email class /system/libraries/Email.php

CI Email helper class /system/helpers/email_helper.php



  1. can you show an example using a newline?

  2. Hey, I had a great time reading your website. Do you have an email address that I can contact you on? Thank you and hope to hear from you soon.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: