Sending Email in CodeIgniter


Sending email in CodeIgniter is much easier and you can set preferences as per your needs. CodeIgniter provides an Email library to sending email in application. CodeIgniter’s Email class provides the following features.

  • Multiple Protocols: Mail, Sendmail, and SMTP
  • Multiple recipients
  • CC and BCCs
  • HTML or Plaintext email
  • Attachments
  • Word wrapping
  • Priorities
  • BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
  • Email Debugging tools

In this tutorial, we’ll show the mostly used email features for the web project. Using our sample code you can send a text email, HTML email, and email with an attachment. Also, you would be able to set the cc email address(s) and bcc email address(s).

To send an email in CodeIgniter, you have to load the email library first. Use the following line of code to load the CodeIgniter’s Email library.


Send Text Email:

Here is an example code to send text email in CodeIgniter.

$this->email->subject('Test Email (TEXT)');
$this->email->message('Text email testing by CodeIgniter Email library.');

Send HTML Email:

To send an email with HTML content, set a preference (mailtype) by passing an array of preference value (html) to the email initialize function. Here is an example code to send HTML email in CodeIgniter.

$htmlContent '<h1>HTML email testing by CodeIgniter Email Library</h1>';
$htmlContent .= '<p>You can use any HTML tags and content in this email.</p>';
$config['mailtype'] = 'html';
$this->email->subject('Test Email (HTML)');

Send Email with Attachment:

Use attach() function of Email class to attach files in email. Here is an example code to send an email with attachment in CodeIgniter.

$htmlContent '<h1>HTML email with attachment testing by CodeIgniter Email Library</h1>';
$htmlContent .= '<p>You can attach the files in this email.</p>';

$config['mailtype'] = 'html';
$this->email->subject('Test Email (Attachment)');

Send Email to Multiple Recipient(s):

Using to() function of Email class, you can send email to single or multiple recipient(s). Provide single email, comma-delimited list or an array.





$recipientArr = array('''''');

Sets the CC and BCC Email Address(s):

Just like to(), you can provide single email, comma-delimited list or an array in cc() and bcc().


Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You


  1. ZoneTM Said...
  2. Braj Said...

Leave a reply