Codeigniter Email Class Library

Codeigniter Email Class Library – Email class library provide various functions that are used to send email. It can be done by protocal. $this->load->library(’email’); is used to load the library. Here in this tutorial, we are going to explain how to use email class library.

Codeigniter email class library | Load | Example

Let us understand how file email class works in codeigniter with examples.

Load codeigniter email class library

How to load email class library:



There are following functions available in email class library. Now let us go through one by one to understand.

  • 1. Sending email
  • 2. Setting email preferences

1. Sending email


Here is simple example of sending email.

Sending email in codeigniter example:-

public function sendmail()
$from = $this->email->from('sonukr321@gmail', 'sonu kumar');
$to = $this->email->to('');
$cc = $this->email->cc('');
$bcc = $this->email->bcc('');

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

if($this->email->send($from, $to, $cc, $bcc, $subject, $message))
	echo "sent succesfully";
	echo "unable to send";

2. Setting email preferences


Here is simple example of setting email preferences.

Setting email preferences in codeigniter example:-

$config['protocol'] = 'sendmail';
$config['mailtype'] = 'text';
$config['smtp_timeout'] = '5';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

There are 21 different preferences availables.

Class reference

There are many types of class reference in email class library.

1. From

from($from[$name = ''[$return_path = NULL]])

This function is used to set email address and name of sender.

2. Reply to

reply_to($replyto[$name = ''])

This function is used to set reply to address.

3. To


We use this function to set email address of reciever. address can be single or multiple both.

4. CC


We use this function to set multiple email address of reciever.

5. Subject


This function is used to set email subject.

6. Message


This function is used to set email message body.

7. Set alt message


This function is used to set alternative email message body.

8. Set header

set_header($header, $value)

This function is used to add additional header.

9. Clear

clear([$clear_attachments = FALSE])

This function is used to clear all attachment files.

10. Send

send([$auto_clear = TRUE])

This method is used to sending the email.

11. Attachment

attach($filename[$disposition = ''[$newname = NULL[$mime = '']]])

We use this function to add multiple attachment files.

12. Print debugger

print_debugger([$include = array('headers', 'subject', 'body')])

By using this function we can print any type of error in email class.


Add Comment

📖 Read More