Integrating Sendgrid with CodeIgniter

SendGrid

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 – http://sendgrid.com/

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

/application/config/email.php

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’] = ‘smtp.sendgrid.net’;
$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();
$this->ci->load->library(’email’);

}

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

$this->ci->email->from(‘hello@yourdomain.com’, ‘Your App Name’);
$this->ci->email->to($email);

$this->ci->email->subject($subject);
$this->ci->email->message($message);

$this->ci->email->send();

}

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

function send_invitee_message_after_requesting_invite($email){

$subject = “Your invitation confirmation”;
$message = “Thanks for requesting an invite!”;
$this->send_email($email,$subject,$message);

}

……

Inside your controller where you wish to send the emails –

……

// SEND EMAIL TO A USER WHO REQUESTED AN INVITE

$email = “test@abc.com”;

$this->load->library(‘Email_Assistant’);
$email_assistant = new Email_Assistant();
$email_assistant->send_invitee_message_after_requesting_invite($email);
……

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

Advertisements

3 comments

  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.

    Regards,

    Joel
    JHouston791 gmail.com

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: