May 12, 2015

Sending Email from CodeIgniter with Mandrill

The last time I used CodeIgniter in my project was about two years ago. It was an online property marketplace where people can collectively buy expensive houses by sharing ownership of the house and, of course, sharing the mortgage. There was these times when I had my dead-end building an application module to send email to customers, some sort of news letter. The dead-end was actually due to google SMTP limitation on relaying email from my application. If you are on a project that still rely on Google's SMTP to send email, I suggest you migrate to Mandrill.

Mandrill

/ˈmændrəl/ | noun | is a transactional email platform from MailChimp. By definition Mandrill is a transactional email platform, which can use to relay emails from our application. Of course there is a price for their services, but as long as we are sending less than 12,000 emails per month we are eligible for free services. It is easy though to scale up if we need to send more than 12,000 emails in a month. Just upgrade your package without re-signing-up your account.

CodeIgniter Mail Library

As our favorite PHP framework, CodeIgniter has the ability to connect to Mandrill as a relay to our mailing module. Check inside folder application/config/ and if you cannot find a file named email.php, create one right now. Now we have to write the CodeIgniter-Mandrill configuration like this:

<?php
$config['protocol'] = 'smtp';
$config['charset']  = 'iso-8859-1'; //Change this you your preferred charset 
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html'; //Use 'text' if you don't need html tags and images

$config['smtp_host'] = '#';
$config['smtp_user'] = '#';
$config['smtp_pass'] = '#';
$config['smtp_port'] = '#';
For now, we keep the email configuration as above. But we will come back later to fill in the hash-tags after I show you how to get them from Mandrill. Meanwhile, let's go write some code to send emails. Open up any controller file and add this function at the end of the controller.
public function send_now()
{
    $this->load->library('email');

    $this->email->from('my-original@email.com', 'Your Name');
    $this->email->to('your-destination@email.com');
    $this->email->cc('another-destination@email.com');
    $this->email->bcc('blinded-destination@email.com');

    $this->email->subject('Sending Email from CodeIgniter with Mandrill');
    $this->email->message('If you forgot how to do this, go ahead and refer to: <a href="http://the-amazing-php.blogspot.com/2015/05/codeigniter-send-email-with-mandrill.html">The Amazing PHP</a>.');

    $this->email->send();
}
That simple, right!

Now, Back to Mandrill!

In order to get our mailing module work, we need to register to mandrill to get our SMTP credentials. To do so, go ahead and open your favorite browser (mine is Chrome) and enter http://www.mandrill.com at the address bar.
Click the Sign Up button to create a new account on Mandrill. This is very easy because all you need to fill is just your email address and your Mandrill password. Note that your mandrill password is not your SMTP password.
After signing up, there is going to be an optional profile form that you may or may not fill. I will skip this form for now by clicking the No, Thanks button on the left bottom of the form.
Next step is to click the Get SMTP Credentials button.
And then type in your Mandrill password when prompted.
Mandrill will create and display your SMTP account detail afterward. Now, to get your SMTP password, click on the + Add API Key button below your SMTP credentials.
Up to this point you should see a new API key. This key (or one of these keys, if you have more than one) can be your SMTP password.
Go on and replace those hash-tags on our email configuration. Go to directory application/config/ and open file email.php, then edit the file matching your Mandrill SMTP credentials.
$config['smtp_host'] = 'smtp.mandrillapp.com';
$config['smtp_user'] = 'my-email@address.com';
$config['smtp_pass'] = 'th1s-1s-y0ur-p4ssw0rd!';
$config['smtp_port'] = '587';

That's it!
Now try to run your script on your browser, and give a comment below if you're having trouble. By the way, to prove you that this tutorial works (even from http://localhost setup on your computer), this what it looks like when I checked my email:
You can also download my sample source code by clicking here.

1 comment: