CodeIgniter - Sending Email

CodeIgniter - Sending Email

CodeIgniter - Sending Email

Sending email in CodeIgniter is much easier. You also configure email settings in CodeIgniter. CodeIgniter provides the following features for sending emails −

  • Multiple Protocols - Mail, Sendmail and SMTP
  • TLS and SSL encryption for SMTP
  • Multiple recipients
  • Central Committee and OCC
  • HTML or plain text email
  • Attachments
  • Hyphenation
  • priorities
  • BCC batch mode, allowing you to split large lists of email addresses into small BCC batches.
  • Email Debugging Tools

The email class has the following functions to make it easier for you to send email.

SN

Syntax

parameters

Return

return type

one

from( $from [, $name=" [, $return_path=NULL ]])

$from ( string ) - "From" email address

$name ( string ) - display name "From"

$return_path ( string ) - optional email address to redirect undelivered email to

CI_Email instance (method chain)

CI_Email

2

reply_to( $replyto [, $name=" ])

$replyto ( string ) - email address for replies

$name ( string ) - Display name for the reply email address

CI_Email instance (method chain)

CI_Email

2

k ( $ k )

$ to ( mixed ) - comma-separated string or array of email addresses

CI_Email instance (method chain)

CI_Email

3

cube cm ( $ cc )

$ cc ( mixed ) - comma-separated string or array of email addresses

CI_Email instance (method chain)

CI_Email

4

bcc( $bcc [, $limit=" ])

$ bcc ( mixed ) - comma-delimited string or array of email addresses

$limit ( int ) - Maximum number of emails sent per batch

CI_Email instance (method chain)

CI_Email

5

subject ( $ subject )

$subject ( string ) - subject line of the email

CI_Email instance (method chain)

CI_Email

6

message ( $body )

$body ( string ) - the body of the email message

CI_Email instance (method chain)

CI_Email

7

set_alt_message( $str )

$ str ( string ) - alternate email message body

CI_Email instance (method chain)

CI_Email

eight

set_header( $header, $value )

$ header ( string ) — Header name

$value ( string ) - header value

CI_Email instance (method chain)

CI_Email

9

clear([ $clear_attachments = FALSE ])

$clear_attachments ( bool ) - clear attachments or not

CI_Email instance (method chain)

CI_Email

10

send([ $auto_clear = TRUE ])

$ auto_clear ( bool ) - Automatically clear message data

CI_Email instance (method chain)

CI_Email

eleven

attach($filename[, $disposition="[, $newname=NULL[, $mime="]]])

$filename ( string ) — Filename

$disposition ( string ) - 'location' of the attachment. Most email clients make their decision regardless of the MIME specification used here.

$newname ( string ) — Custom filename to use in email

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

CI_Email instance (method chain)

CI_Email

12

attachment_cid( $filename )

$filename ( string ) - the existing filename of the attachment

Attachment Content-ID or FALSE if not found

line

$from ( string ) - "From" email address

$name ( string ) - display name "From"

$return_path ( string ) - optional email address to redirect undelivered email to

$replyto ( string ) - email address for replies

$name ( string ) - Display name for the reply email address

$ to ( mixed ) - comma-separated string or array of email addresses

$ cc ( mixed ) - comma-separated string or array of email addresses

$ bcc ( mixed ) - comma-delimited string or array of email addresses

$limit ( int ) - Maximum number of emails sent per batch

$subject ( string ) - subject line of the email

$body ( string ) - the body of the email message

$ str ( string ) - alternate email message body

$ header ( string ) — Header name

$value ( string ) - header value

$clear_attachments ( bool ) - clear attachments or not

$ auto_clear ( bool ) - Automatically clear message data

$filename ( string ) — Filename

$disposition ( string ) - 'location' of the attachment. Most email clients make their decision regardless of the MIME specification used here. IANA

$newname ( string ) — Custom filename to use in email

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

$filename ( string ) - the existing filename of the attachment

Sending email

To send an email using CodeIgniter, you must first download the email library using the following:

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

After downloading the library, just follow the below functions to install the necessary elements to send an email. The from() function is used to specify - where the email is being sent from and for the () function - to whom the email is being sent. The subject() and message() function is used to set the subject and message of the email.

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

$this->email->to('someone@example.com');

 

$this->email->subject('Email Test');

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

After that, execute the send() function as shown below to send the email.

$this->email->send();

example

Create a controller file Email_controller.php and save it to application/controller/Email_controller.php .

Create a view file named email_form.php and save it to application/views/email_form.php

 

  

      <metacharset="utf-8">

     

  

        

  

     

                 

     

     

                 

     

  

        

Make changes to the rout.php file in application/config/rout.php and add the following line to the end of the file.

$route['email'] = 'Email_Controller';

Run the above example by visiting the following link. Replace yoursite.com with your site's URL.

http://yoursite.com/index.php/email