Codeigniter - SPLessons

CodeIgniter Send Email

Home > Lesson > Chapter 25
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Send Email

CodeIgniter Send Email

shape Description

Email is a Electronic mail through which messages can be sent to multiple recipients. Here in the tutorial "CodeIgniter Send Email", usage of default class of CodeIgniter email and CodeIgniter email library in sending emails through various protocols like mail, SMTP, and Sendmail will be used. Email library in CodeIgniter will be helpful in using CC and BCCs, plain text or HTML code and multiple recipients. Before going to the example, few important parameters that are mostly used are discussed. Parameters of CodeIgniter Email:
Parameter Structure Meaning
From $from (string) From Address is defined
reply_to $replyto (string) Replies to a provided address is defined
To to($to) Indicates recipient address(es)
CC cc($cc) Indicates array of recipient address(es)
BC bcc($bcc[, $limit = ”]) $bcc (mixed) − Comma-delimited string or an array of e-mail addresses. $limit (int) represents number of emails to send per batch
Subject $subject (string) Indicates email subject
Message $body (string) Indicates Body of Email
Header set_header($header, $value) Indicates Email header
Attachment attach($filename[, $disposition = ”[, $newname = NULL[, $mime = ”]]]) $filename(string) Indicates name of file $disposition (string) − indicates ‘disposition’ of the attachment. $newname (string) − indicates Custom file name to use in the e-mail $mime (string) − Indicates MIME type to use

shape Example

"CodeIgniter Send Email" chapter gives an example on how to send email. Initially, Create the controller ci_email_tutorial.php and enter the below code. [php] <?php if (!defined('BASEPATH'))exit('No direct script access allowed'); class CI_Email_Tutorial extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('form'); $this->load->library('form_validation'); $this->load->library('encrypt'); } // Show email page public function index() { $this->load->view('view_form'); } // Send Gmail to another user public function Send_Mail() { // Check for validation $this->form_validation->set_rules('user_email', 'User Email', 'trim|required|xss_clean'); $this->form_validation->set_rules('user_password', 'User Password', 'trim|required|xss_clean'); $this->form_validation->set_rules('to_email', 'To', 'trim|required|xss_clean'); $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean'); $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean'); if ($this->form_validation->run() == FALSE) { $this->load->view('view_form'); } else { // Storing submitted values $sender_email = $this->input->post('user_email'); $user_password = $this->input->post('user_password'); $receiver_email = $this->input->post('to_email'); $username = $this->input->post('name'); $subject = $this->input->post('subject'); $message = $this->input->post('message'); // Configure email library $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.googlemail.com'; $config['smtp_port'] = 465; $config['smtp_user'] = $sender_email; $config['smtp_pass'] = $user_password; // Load email library and passing configured values to email library $this->load->library('email', $config); $this->email->set_newline("\r\n"); // Sender email address $this->email->from('pkrock00@gmail.com', $username); // Receiver email address $this->email->to($receiver_email); // Subject of email $this->email->subject($subject); // Message in email $this->email->message($message); if ($this->email->send()) { $data['message_display'] = 'Email Successfully Send !'; } else { $data['message_display'] = '<p class="error_msg">Invalid Gmail Account or Password !</p>'; } $this->load->view('view_form', $data); } } } ?> [/php] Then create the view called view_form.php in the path application/views and enter the below code. [html] <html> <head> <title>Codeigniter Email</title> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css"> <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'> </head> <body> <div class="main"> <div id="content"> <h2 id="form_head">Codelgniter Email</h2><br/> <hr> <div id="form_input"> <div class="msg"> <?php if (isset($message_display)) { echo $message_display; } ?> </div> <?php echo '<div class="error_msg">'; echo validation_errors(); echo "</div>"; echo form_open('ci_email_tutorial/send_mail'); echo form_label('Email-ID'); echo "<div class='all_input'>"; $data_email = array( 'type' => 'email', 'name' => 'user_email', 'id' => 'e_email_id', 'class' => 'input_box', 'placeholder' => 'Please Enter Email' ); echo form_input($data_email); echo "</div>"; echo "<br/>"; echo form_label('Password'); echo "<div class='all_input'>"; $data_password = array( 'name' => 'user_password', 'id' => 'password_id', 'class' => 'input_box', 'placeholder' => 'Please Enter Password' ); echo form_password($data_password); echo "</div>"; echo "<br/>"; echo form_label('Name'); echo "<div class='all_input'>"; $data_email = array( 'name' => 'name', 'class' => 'input_box', 'placeholder' => 'Please Enter Name' ); echo form_input($data_email); echo "</div>"; echo "<br/>"; echo form_label('To'); echo "<div class='all_input'>"; $data_email = array( 'type' => 'email', 'name' => 'to_email', 'class' => 'input_box', 'placeholder' => 'Please Enter Email' ); echo form_input($data_email); echo "</div>"; echo "<br/>"; echo form_label('Subject'); echo "<div class='all_input'>"; $data_subject = array( 'name' => 'subject', 'class' => 'input_box', 'placeholder' => 'Subject' ); echo form_input($data_subject); echo "</div>"; echo "<br/>"; echo form_label('Message'); echo "<div class='all_input'>"; $data_message = array( 'name' => 'message', 'rows' => 5, 'cols' => 32, 'placeholder' => 'Write Message Here...' ); echo form_textarea($data_message); echo "</div>"; echo "<br/>"; ?> </div> <div id="form_button"> <?php echo form_submit('submit', 'Send', "class='submit'"); ?> </div> <?php echo form_close(); ?> </div> </div> </body> </html> [/html] Output:

Summary

shape Key Points

CodeIgniter Send Email chapter draws out following important points:
  • CodeIgniter email library will send the emails through various protocols like mail, SMTP, and Sendmail.