Codeigniter Sending email via SMTP Gmail

Here I share with you the way; How to send email in Codeigniter Framework via Gmail SMTP feature.

Write this code in your controller:-


public function send_email() {

        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://',
            'smtp_port' => 465,
            'smtp_user' => '', // gmail Username
            'smtp_pass' => 'YOUR_GMAIL_PASSWORD', // gmail Password
            'mailtype' => 'html', // what type of mail you want to sent. i.e html/plaintext
            'charset' => 'utf-8',
            'wordwrap' => TRUE

        $message = "Hi, This is a test mail using gmail smtp from my PC using CodeIgniter Framework.";

        $this->load->library('email', $config);
        $this->email->from('FROM_EMAIL_ADDRESS'); // in which email address you want to send email.
        $this->email->to('TO_EMAIL_ADDRESS'); // where your mail goes.
        $this->email->subject('Test Email via Gmail SMTP from My PC');
        $success = $this->email->send();

        if ($success) {
            echo 'Waooo.. Email send successfully!!!';
        } else {
            // if email don't send then shows error.

CodeIgniter jQuery Ajax live search

View file –

<div class="something">
     <input name="search_data" id="search_data" type="text" onkeyup="ajaxSearch();">
     <div id="suggestions">
         <div id="autoSuggestionsList"></div>

Javascript Code –

<script type="text/javascript">

function ajaxSearch()
    var input_data = $('#search_data').val();

    if (input_data.length === 0)

        var post_data = {
            'search_data': input_data,
            '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'

            type: "POST",
            url: "<?php echo base_url(); ?>welcome/autocomplete/",
            data: post_data,
            success: function (data) {
                // return success
                if (data.length > 0) {


Controller Function – [Welocome]

public function autocomplete()
     // load model

     $search_data = $this->input->post('search_data');

     $result = $this->Welcome_model->get_autocomplete($search_data);

     if (!empty($result))
          foreach ($result as $row):
               echo "<li><a href='#'>" . $row->name . "</a></li>";
           echo "<li> <em> Not found ... </em> </li>";

Model Function – [Welcome_model]

SELECT id, name FROM `fruits`
public function get_autocomplete($search_data)
    $this->db->select('name, id');
    $this->db->like('name', $search_data);

    return $this->db->get('fruits', 10)->result();

A little bit styling –

input[type=text] {
    width: 200px;
    padding: 5px;
    margin: 5px 0;
    box-sizing: border-box;

#autoSuggestionsList > li {
    background: none repeat scroll 0 0 #F3F3F3;
    border-bottom: 1px solid #E3E3E3;
    list-style: none outside none;
    padding: 3px 15px 3px 15px;
    text-align: left;

#autoSuggestionsList > li a { color: #800000; }

.auto_list {
    border: 1px solid #E3E3E3;
    border-radius: 5px 5px 5px 5px;
    position: absolute;


You can download the whole CodeIgniter Code from the github repo.


If it doesn’t work or you face any error to implement the code, Please send me a mail ( I will try to respond you.

Get the Last added ID and Insert it to another Table as a foreign key

Get the last id from the database:-


Suppose I have two table ‘product_sale’ and ‘customer_information’. I want to get last id from the ‘product_sale’ table and insert it to ‘customer_information’ table as a foreign key.

public function insertProductsale($data, $data_c) {

$this->db->insert('product_sale', $data);
$sale_id = $this->db->insert_id();
$data_c['sale_id'] = $sale_id;
return $this->db->insert('customer_information', $data_c);

CodeIgnier pagination CSS Style

We always use pagination in most of the part of a Custom CMS and other where you need. But sometime we don’t have enough time to create a nice style in the pagination links. Now I just share with all to some style css code. So just copy and paste and got a nice style in Pagination Anchor tag.

<style type="text/css">

#pagination a, #pagination strong {
background: #e3e3e3;
padding: 4px 7px;
font-size: 13px;
text-decoration: none;
border: 1px solid #eac9c9;
color: #292929;

#pagination strong, #pagination a:hover{
font-weight: normal;
background: #AFAFAF;

table {
margin-bottom: 30px;


<div id="pagination">
echo $this->pagination->create_links();