Archivo de la categoría: General

How to: Send transactional emails with Mandrill and CodeIgniter using SMTP


Assuming you have already setted your account in Mandrill, lets configure your CodeIgniter application to send transactional emails.

First of all, save your email configuration file. As any other configuration file, it must be in the application/config folder. So, the contents of ./application/config/email.php would be:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * Configuration file for Email library
 */
$config['protocol'] = 'smtp';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['smtp_timeout'] = 5;
$config['smtp_host'] = 'smtp.mandrillapp.com';
$config['smtp_port'] = 587;
$config['smtp_user'] = $_ENV['MANDRILL_USERNAME'];
$config['smtp_pass'] = $_ENV['MANDRILL_PASSWORD']; // It's your API key generated.

You can set custom headers provided by Mandrill in your Email library. For instance, inside the send() function:

$this->_set_header('X-MC-AutoText', 'TRUE');

I don’t recommend overriding or extending the Email class when using the general configuration file.

As usual, you have to use the email functions as stated in the CodeIgniter Documentation:

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

echo $this->email->print_debugger();

NOTE: If your getting “Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.” error message, don’t look for pear inclusions or Mandrill auth keys errors, just check your iptables or your firewall and enable the SMTP port.