Sending Email in CodeIgniter

sending-email-in-codeigniter-by-codexworld

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.

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

Send Text Email:

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

$this->email->to('recipient@example.com');
$this->email->from('codexworld@gmail.com','CodexWorld');
$this->email->subject('Test Email (TEXT)');
$this->email->message('Text email testing by CodeIgniter Email library.');
$this->email->send();

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->initialize($config);
$this->email->to('recipient@gmail.com');
$this->email->from('codexworld@gmail.com','CodexWorld');
$this->email->subject('Test Email (HTML)');
$this->email->message($htmlContent);
$this->email->send();

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->initialize($config);
$this->email->to('recipient@gmail.com');
$this->email->from('codexworld@gmail.com','CodexWorld');
$this->email->subject('Test Email (Attachment)');
$this->email->message($htmlContent);
$this->email->attach('files/attachment.pdf');
$this->email->send();

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.

$this->email->to('one@example.com');

OR

$this->email->to('one@example.com, two@example.com, three@example.com');

OR

$recipientArr = array('one@example.com''two@example.com''three@example.com');
$this->email->to($recipientArr);

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().

$this->email->cc('another@example.com');
$this->email->bcc('another@example.com');

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

4 Comments

  1. Umesh Vishwakarma Said...
  2. Anugrah Singh Said...
  3. ZoneTM Said...
  4. Braj Said...

Leave a reply

keyboard_double_arrow_up