CodeIgniter Sending Email

CodeIgniter's Email Class supports the following features:

  • Multiple Protocols: Mail, Sendmail, and SMTP.
  • TLS and SSL Encryption for 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.

Email Preferences

Preference Default Value Options Description
useragent CodeIgniter None The "user agent".
protocol mail mail, sendmail, or smtp The mail sending protocol.
mailpath /usr/sbin/sendmail None The server path to Sendmail.
smtp_host No Default None SMTP Server Address.
smtp_user No Default None SMTP Username.
smtp_pass No Default None SMTP Password.
smtp_port 25 None SMTP Port.
smtp_timeout 5 None SMTP Timeout (in seconds).
smtp_keepalive FALSE TRUE or FALSE (boolean) Enable persistent SMTP connections.
smtp_crypto No Default tls or ssl SMTP Encryption
wordwrap TRUE TRUE or FALSE (boolean) Enable word-wrap.
wrapchars 76   Character count to wrap at.
mailtype text text or html Type of mail. If you send HTML email you must send it as a complete web page. Make sure you don’t have any relative links or relative image paths otherwise they will not work.
charset $config['charset']   Character set (utf-8, iso-8859-1, etc.).
validate FALSE TRUE or FALSE (boolean) Whether to validate the email address.
priority 3 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal.
crlf n "rn" or "n" or "r" Newline character. (Use "rn" to comply with RFC 822).
newline n "rn" or "n" or "r" Newline character. (Use "rn" to comply with RFC 822).
bcc_batch_mode FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode.
bcc_batch_size 200 None Number of emails in each BCC batch.
dsn FALSE TRUE or FALSE (boolean) Enable notify message from server

Sending an Email

To send an email, first you have to load email library.

$this->load->library('email'); // load email library
$this->email->from('your@example.com', 'Your Name'); // Sets the email address and name of the person sending the email
$this->email->to('someone@example.com'); // Sets the email address(s) of the recipient(s). Can be a single e-mail, a comma-delimited list or an array
$this->email->subject('Email Test'); // Sets the email subject
$this->email->message('Testing the email class.'); // Sets the e-mail message body
$this->email->send(); // e-mail sending method. Returns boolean TRUE or FALSE based on success or failure

Using a text editor, create a controller called Email.php. In it, place this code and save it to your application/controllers/ directory:

<?php 
   class Email extends CI_Controller { 
 
      function __construct() { 
         parent::__construct(); 
         $this->load->library('email'); 
         $this->load->helper('form'); 
      } 
      
      public function index() { 
         $this->load->view('email_form'); 
      } 
  
      public function send_mail() { 
         $to_email = $this->input->post('email'); 
   
         $this->email->from("your@example.com", 'Your Name'); 
         $this->email->to($to_email);
         $this->email->subject('Email Test'); 
         $this->email->message('Testing the email class.'); 
   
         //Send mail 
         if($this->email->send()) 
         $msg = "Email sent successfully."; 
         else 
         $msg = "Error occured during sending email."; 
         $this->load->view('email_form', compact('msg')); 
      } 
   } 
?>

Using a text editor, create a view called email_form.php. In it, place this code and save it to your application/views/ directory:

<!DOCTYPE html> 
<html lang = "en">
   <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter Email Example</title> 
   </head>
   <body> 
      <?php
         if (isset($msg))    
         echo $msg; 
         echo form_open('/email/send_mail'); 
      ?> 
      <input type="email" name="email" required /> 
      <input type="submit" value="Submit" /> 
      <?php 
         echo form_close(); 
      ?> 
   </body>
</html>

To try your form, visit your site using a URL similar to this one:

http://localhost/codeigniter/index.php/email/index