In this CodeIgniter Sending Email tutorial we are going to learn how to send emails in codeigniter. It is much easier to send email in CodeIgniter. You can also set the email preferences. CodeIgniter offers the following sending features −

  • 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
  • Multiple Protocols − Mail, Sendmail, and SMTP
  • TLS and SSL Encryption for SMTP
  • Multiple recipients
  • CC and BCCs

Email classes have the following features to simplify email sending.

S.N. Syntax Parameters Return Return Type
01 from($from[, $name = ”[, $return_path = NULL]]) $from (string) − “From” e-mail address

$name (string) − “From” display name

$return_path (string) − Email address optional to redirect undelivered email to

CI_Email instance (method chaining) CI_Email
02 reply_to($replyto[, $name = ”]) $replyto (string) − E-mail address for replies

$name (string) − Display name for the reply-to e-mail address

CI_Email instance (method chaining) CI_Email
03 to($to) $to (mixed) − Comma-delimited string or an array of e-mail addresses CI_Email instance (method chaining) CI_Email
04 cc($cc) $cc (mixed) − Comma-delimited string or an array of e-mail addresses CI_Email instance (method chaining) CI_Email
05 bcc($bcc[, $limit = ”]) $bcc (mixed) − Comma-delimited string or an array of e-mail addresses

$limit (int) − Maximum number of e-mails to send in per batch

CI_Email instance (method chaining) CI_Email
06 subject($subject) $subject (string) − E-mail subject line CI_Email instance (method chaining) CI_Email
07 message($body) $body (string) − E-mail message body CI_Email instance (method chaining) CI_Email
08 set_alt_message($str) $str (string) − Alternative e-mail message body CI_Email instance (method chaining) CI_Email
09 set_header($header, $value) $header (string) − Header name

$value (string) − Header value

CI_Email instance (method chaining) CI_Email
10 clear([$clear_attachments = FALSE]) $clear_attachments(bool) – Clear attachments CI_Email instance (method chaining) CI_Email
11 send([$auto_clear = TRUE]) $auto_clear (bool) − Whether to clear message data automatically CI_Email instance (method chaining) CI_Email
12 attach($filename[, $disposition = ”[, $newname = NULL[, $mime = ”]]]) $filename (string) − File name

$disposition (string) − “disposition” of the attachment. Most email customers decide for themselves regardless of the MIME specifications used here. iana

$newname (string) − Custom file name to use in the e-mail

$mime (string) − MIME type to use (useful for buffered data)

CI_Email instance (method chaining) CI_Email
13 attachment_cid($filename) $filename (string) − Existing attachment filename Attachment Content-ID or FALSE if not found string

To send an e-mail using CodeIgniter, you must first load your e – mail library using −

/* == Loading Email Library == */
$this->load->library('email');

After you have loaded the library, simply execute the following functions to set the elements necessary for sending an email.

  • from() is used to set, from where the email is being sent.
  • to() is used, to whom the email is being sent.
  • subject() and message() is used to set, the subject and message of the email.

You can get idea from the following code.

$this->email->from('admin@tutorialpace.com', 'Cheif Editor');
$this->email->to('info@tutorialpace.com');
 
$this->email->subject('Provide Information on CodeIgniter Tutorials');
$this->email->message('Testing the email class in CodeIgniter Sending Email Tutorial.');

Execute following method to send email.

/* == Email will be execute on this method == */
$this->email->send();