Codeigniter – Sending Emails using Gmail’s SMTP Server

Written by Sid Vel

Topics: Codeigniter

Sending emails and attaching files along with them are super easy! Just create a file called email.php in the config folder of your application. This file will contain the details of your Gmail SMTP server.

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://';
$config['smtp_port'] = '465';
$config['smtp_user'] = '';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";

In your Email controller, create a function with the following code…

class Email extends CI_Controller

  function send()
    // Loads the email library
    // FCPATH refers to the CodeIgniter install directory
    // Specifying a file to be attached with the email
    $file = FCPATH . 'license.txt';
    // Defines the email details
    $this->email->from('', 'My Name');
    $this->email->subject('Email Test);
    $this->email->message('Testing the email class.');
    // The email->send() statement will return a true or false
    // If true, the email will be sent
    if ($this->email->send()) {
      echo "All OK";
    } else { 
      echo $this->email->print_debugger();


The above example is very basic, but explains how to send emails using Google Mail’s SMTP server.

Leave a Comment Here's Your Chance to Be Heard!