Tuesday, March 4, 2014

How To Send Email With An Attachment In Codeigniter?

You need to use following two CodeIgniter's robust classes.
1. Email Library : CodeIgniter's robust Email Class library to make sending email very simple and easy.
2. Path Helper : The Path Helper file contains functions that permits you to work with file paths on the server.

Here is a basic example demonstrating how you might send email with attachment. Note: This example assumes you are sending the email from one of your controllers.

$this->email->from('your@example.com', 'Your Name');

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

/* This function will return a server path without symbolic links or relative directory structures. */
$path = set_realpath('uploads/pdf/');
$this->email->attach($path . 'yourfile.pdf');  /* Enables you to send an attachment */


echo $this->email->print_debugger();

The above email sending example is used in CodeIgniter User Guide Version 2.1.4.