Course Content
Introduction to CodeIgniter
CodeIgniter is a powerful PHP framework built for developers who need a simple and elegant toolkit to create full-featured web applications.
0/3
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
0/1
Forms and Input
Forms provide a way for users to interact with the application and submit data.
0/1
Composer
Composer is dependency manager in PHP. it allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
0/1
Security
You can enable CSRF protection by modifying your application/config/config.php file
0/1
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
DataTable
DataTables is a table enhancing plug-in for the jQuery Javascript library that helps in adding sorting, paging, and filtering abilities to plain HTML tables with minimal effort. The main goal is to enhance the accessibility of data in normal HTML tables.
0/1
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
Payment Gateway
Razorpay and PayTM Payment Gateway
0/2
Chatbot
WhatsApp Chatbot and Telegram Chatbot
0/2
CodeIgniter 3
    About Lesson

    In CodeIgniter 3, sending emails can be enhanced using the external library PHPMailer, which provides a more powerful and flexible way to handle email operations than the built-in email class. This tutorial will guide you through the steps to set up PHPMailer in CodeIgniter 3, send emails using it, and create a form for composing emails.

     

    Step 1: Install PHPMailer Using Composer

    To use PHPMailer in your CodeIgniter 3 project, you need to install it using Composer. Navigate to your project’s root directory and run the following command:

    Example:

    composer require phpmailer/phpmailer

     

    This command will download PHPMailer into your project’s vendor directory.

     

    Step 2: Configure Composer Autoload in CodeIgniter

    After installing PHPMailer, you need to enable Composer’s autoload functionality in CodeIgniter. Open the config/config.php file and change the composer_autoload setting:

    1. Add the following lines in config/config.php

    $config['composer_autoload'] = FALSE;
    // to
    $config['composer_autoload'] = TRUE;

    This setting will allow CodeIgniter to automatically load Composer dependencies, including PHPMailer.



    Step 3: Create a Controller for Sending Emails

    Create a new controller file named Email.php inside the application/controllers directory. This controller will handle composing and sending emails using PHPMailer.

     

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
     
    use PHPMailerPHPMailerPHPMailer;
    use PHPMailerPHPMailerException;
    
    class Email extends CI_Controller {
        
        public function __construct() {
    		parent::__construct();
        }
        
        public function compose() {
        
            $this->load->view('compose');
        
        }
        
        public function send_email() {
        
            $email          = $this->input->post('email');
            $subject        = $this->input->post('subject');
            $message        = $this->input->post('message');
            
            $mail = new PHPMailer(true);  
    		try {
    		    
    		    $mail->isSMTP();  
    		    $mail->Host         = 'mail.infovistar.in'; 
    		    $mail->SMTPAuth     = true;     
    		    $mail->Username     = 'infovitar';  
    		    $mail->Password     = 'inf0v!st@r';
    			$mail->SMTPSecure   = 'tls';  
    			$mail->Port         = 587;  
    			$mail->Subject      = $subject;
    			$mail->Body         = $message;
    			$mail->setFrom('infovistarindia@gmail.com', 'Infovistar');
    			
    			$mail->addAddress($email);  
    			$mail->isHTML(true);      
    			
    			if(!$mail->send()) {
    			    echo "Something went wrong. Please try again.";
    			}
    		    else {
    			    echo "Email has been sent successfully.";
    		    }
    		    
    		} catch (Exception $e) {
    		    echo "Something went wrong. Please try again.";
    		}
            
        }
        
    }

     

    Step 4: Create the Email Composition Form

    Create a compose.php file inside the application/views directory. This file will contain a simple HTML form that allows users to input the recipient’s email, subject, and message.

     

    <form 
        action="<?php echo base_url('email/send_email') ?>" 
    class="form-horizontal" id="add_email_form"
    autocomplete="off" method="post" accept-charset="utf-8"> <div class="form-group"> <label for="inputName" class="col-sm-2 control-label">Recipient Email</label> <div class="col-sm-10"> <input type="hidden" name="id" value="9" id="id" /> <input type="hidden" name="source_id" value="2" id="source_id" /> <input type="text" name="email" value="" id="email" placeholder="Recipient Email Address" class="form-control" required /> </div> </div> <div class="form-group"> <label for="inputName" class="col-sm-2 control-label">Subject</label> <div class="col-sm-10"> <input type="text" name="subject" value="" id="subject" placeholder="Subject" class="form-control" required /> </div> </div> <div class="form-group"> <label for="inputName" class="col-sm-2 control-label">Message</label> <div class="col-sm-10"> <textarea class="form-control" name="message" id="message"></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-info">Send Email</button> </div> </div> </form>

     

    How It Works

    1. Accessing the Form: Open your browser and navigate to http://yourdomain.com/email/compose to access the form for composing an email.
    2. Sending the Email: Fill in the recipient’s email, subject, and message, then click “Send Email.” The data is sent to the send_email method in the Email controller.
    3. PHPMailer Handling: PHPMailer will use the SMTP settings specified in the controller to send the email. If the email is sent successfully, you will see a success message; otherwise, an error message will be displayed.

     

    Using PHPMailer with CodeIgniter 3 is a robust way to manage email functionality in your applications. It provides more control over the email-sending process compared to CodeIgniter’s built-in email class, including better error handling and more configuration options for different SMTP services.