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.
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');
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();
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();
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();
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);
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
best platform for beginers
thanks codexworld team.
Nice Tutorial from this tutorial we simply send email to user.
really awesome, its so detailed
maybe add some disqus comment here
Realling interesting article. I am searching for CodeIgniter Tutorials I looked at this and stood up sometime. Thanks a lot.