Codeigniter Email Library


Codeigniter Email Library: Codeigniter provides email library to send emails. It is very easy to use this library and send emails. You can configure it on the fly. Here in this tutorial we are going to demonstrate how to use email library in codeigniter and send mails.


Codeigniter Email Library | Send Mail Example

First of all we need to load the mail library before using it. We can load library simply as below-

Syntax

Codeigniter Load Email Library Syntax

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

Now you can use it to send mails.

Example

Let us create a simple example to send email-

Codeigniter Send Email Example

$this->load->library('email');
$this->email->from('sender@example.com', 'Your Name');
$this->email->to('receiver@example.com');
$this->email->cc('ccmail@example.com');
$this->email->bcc('bcc@example.com');
$this->email->subject('Codeigniter Email Test');
$this->email->message('This is test mail.');
$this->email->send();

Learn More

Let us have look over more examples.


Send Attachments

You can also attach some file in email using the following method –

Codeigniter Send Email Example

$this->load->library('email');
$this->email->from('sender@example.com', 'Your Name');
$this->email->to('receiver@example.com');
$this->email->cc('ccmail@example.com');
$this->email->bcc('bcc@example.com');
$this->email->subject('Codeigniter Email Test');
$this->email->message('This is test mail.');
$this->email->attach('http://example.com/myfile.pdf');
// or just give file path $this->email->attach('path/myfile.pdf');
$this->email->send();

Send Inline Attachments

If you want to send inline attachments you can use $this->email->attachment_cid() to send inline attachments-

Codeigniter Send Inline(image) Attachments

$this->load->library('email');
$this->email->from('sender@example.com', 'Your Name');
$this->email->to('receiver@example.com');
$this->email->cc('ccmail@example.com');
$this->email->bcc('bcc@example.com');
$this->email->subject('Codeigniter Email Test');
$this->email->message('This is test mail.');
$filename = "images/profile.png";
$cid = $this->email->attachment_cid($filename);
$this->email->message('<img src="cid:'. $cid .'" alt="Image Attachments" />');
$this->email->send();

Advertisements

Add Comment

📖 Read More