Email Class

CodeIgniter's robust Email Class supports 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

Sending Email

Sending email is not only simple, but you can configure it on the fly or set your preferences in a config file.
Here is a basic example demonstrating how you might send email. Note: This example assumes you are sending the email from one of your controllers.

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

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

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

$this->email->send();

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

Setting Email Preferences

There are 17 different preferences available to tailor how your email messages are sent. You can either set them manually as described here, or automatically via preferences stored in your config file, described below:
Preferences are set by passing an array of preference values to the email initialize function.

Here is an example of how you might set some preferences:

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$this->email->initialize($config);

 
Note: Most of the preferences have default values that will be used if you do not set them.


Email Function Reference

$this->email->from()

Sets the email address and name of the person sending the email:
$this->email->from('you@example.com', 'Your Name');

$this->email->reply_to()

Sets the reply-to address. If the information is not provided the information in the "from" function is used. Example:
$this->email->reply_to('you@example.com', 'Your Name');

$this->email->to()

Sets the email address(s) of the recipient(s). Can be a single email, a comma-delimited list or an array:
$this->email->to('someone@example.com'); $this->email->to('one@example.com, two@example.com, three@example.com'); $list = array('one@example.com', 'two@example.com', 'three@example.com');

$this->email->to($list);

$this->email->cc()

Sets the CC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

$this->email->bcc()

Sets the BCC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

$this->email->subject()

Sets the email subject:
$this->email->subject('This is my subject');

$this->email->message()

Sets the email message body:
$this->email->message('This is my message');

$this->email->set_alt_message()

Sets the alternative email message body:

$this->email->set_alt_message('This is the alternative message');
 
This is an optional message string which can be used if you send HTML formatted email. It lets you specify an alternative message with no HTML formatting which is added to the header string for people who do not accept HTML email. If you do not set your own message CodeIgniter will extract the message from your HTML email and strip the tags.

$this->email->clear()

Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles.

foreach ($list as $name => $address)
{
    $this->email->clear();

    $this->email->to($address);
    $this->email->from('your@example.com');
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi '.$name.' Here is the info you requested.');
    $this->email->send();
}

 
If you set the parameter to TRUE any attachments will be cleared as well:

$this->email->clear(TRUE);

$this->email->send()

The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used conditionally:

if ( ! $this->email->send())
{
    // Generate error
}

$this->email->attach()

Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:

$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
$this->email->attach('/path/to/photo3.jpg');

$this->email->send();

$this->email->print_debugger()

Returns a string containing any server messages, the email headers, and the email messsage. Useful for debugging.