Learn Codeigniter

CodeIgniter Email

CodeIgniter's Email Class support following features.

  1. Multiple Protocols support like: Mail, Sendmail, and SMTP
  2. CC and BCCs
  3. HTML or Plaintext email
  4. Attachments
  5. Word wrapping
  6. Email Debugging tools

Example

We will use following step to send mail in CodeIgniter.

Step 1

To Send mail,Create Controller file inside application/controllers/email.php.

<?php 
class email extends CI_Controller{

  function __construct()
{

  parent::__construct();
  $this->load->helper('url');
$this->load->helper('form');
$this->load->library('email');
}
function index()
 {
if($this->input->post('submit'))
{
$config['protocol'] = 'sendmail';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
$from=$this->input->post('name');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$comment=$this->input->post('comment');
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Name:</strong> </td><td>$email</td></tr>";
$message .= "<tr><td><strong>From Mail:</strong> </td><td>$from </td></tr>";
$message .= "<tr><td><strong>Mobile:</strong> </td><td>$mobile</td></tr>";
$message .= "<tr><td><strong>Comment:</strong> </td><td>$comment</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$this->email->from($from);
$this->email->to('[email protected]');
$this->email->cc('[email protected]');
$this->email->bcc('[email protected]');
$this->email->subject('Codeigniter Email Test');
$this->email->message($message);
$this->email->send();
}
$this->load->view('email');
  }
}
?>
Step 2

Create view file inside application\views\email.php to create form.

<html>
<head>
<title>Codeigniter Email  </title>
</head>
<body>
<h3>Codeigniter Email </h3>
<form name="emailform" action="<?php echo  base_url();?>email" method="post">
<p>Name :    <input type="text" name="name"/></p>
<p>Email :    <input type="text" name="email"/></p>
<p>Mobile:   <input type="text" name="mobile"/></p>
<p>Comment: <textarea name="comment"></textarea></p>
<p><input type="submit" name="submit" value="submit"></p>
</form>
</body>
</html>

CodeIgniter Email Function Reference

$this->email->from()

This function is used to set the email address and name of sender.

$this->email->from('[email protected]', 'Sender Name');

$this->email->reply_to()

This function is used to set the reply-to address.

$this->email->reply_to('[email protected]', 'Sender Name');

$this->email->to()

This function is used to set the email address and name of recipient(s). we can

$this->email->to('[email protected]', 'Recipient Name');

$this->email->to()

This function is used to set the email address and name of recipient(s). we can set single email, a comma-delimited list or an array of recipient(s).

$this->email->to('[email protected]', 'Recipient Name'); $this->email->to('[email protected], [email protected], [email protected]'); $arraylist = array('[email protected]', '[email protected]', '[email protected]'); $this->email->to($arraylist);

$this->email->cc()

This function is used to set the CC email address(s). we can set single email, a comma-delimited list or an array of recipient(s).

$this->email->cc('[email protected]');

$this->email->bcc()

This function is used to set the BCC email address(s). we can set single email, a comma-delimited list or an array of recipient(s).

$this->email->bcc('[email protected]');

$this->email->subject()

This function is used to set subject of email.

$this->email->subject('This is My Mail subject');

$this->email->send()

This function return boolean TRUE or FALSE based on success or failure of mail.

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

$this->email->attach()

This function used to send an attachment with email.Note: Use a file path, not a URL.

$this->email->attach('/path/to/one.jpg');
$this->email->attach('/path/to/two.jpg');
$this->email->attach('/path/to/three.jpg');
$this->email->send();