Course Content
Introduction to CodeIgniter 4
CodeIgniter is an Application Development Framework. CodeIgniter is a popular and powerful MVC (Model-View-Controller) framework that is used to develop web applications. It is a free and Open-source PHP framework.
0/5
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.
0/2
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
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
CodeIgniter 4
About Lesson

A PHPMailer library is used to send an email using CodeIgniter. You can install the PHPMailer using Composer.

PHPMailer is a code library used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity with SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming.

Run the following command in the root directory of your project.

Example:

composer require phpmailer/phpmailer

1. Copy and Paste the following lines in app/Config/Routes.php

$routes->add('email/compose', 'Email::compose');
$routes->post('email/send-email', 'Email::send_email');

2. Create a compose.php file in the app/Views/ directory

<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>

3. Create a controller file Email.php in the app/Controllers/ directory.

<?php
namespace App\Controllers;

use App\Controllers\BaseController;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Email extends BaseController {
    
    public function __construct() {
        
    }
    
    public function compose() {
    
        echo view('compose');
    
    }
    
    public function send_email() {
    
        $email          = $this->request->getPost('email');
        $subject        = $this->request->getPost('subject');
        $message        = $this->request->getPost('message');
        
        $mail = new PHPMailer(true);  
        try {
            
            $mail->isSMTP();  
            $mail->Host         = 'host'; //smtp.google.com
            $mail->SMTPAuth     = true;     
            $mail->Username     = 'username';  
            $mail->Password     = 'password';
            $mail->SMTPSecure   = 'tls';  
            $mail->Port         = 587;  
            $mail->Subject      = $subject;
            $mail->Body         = $message;
            $mail->setFrom('username', 'display_name');
            
            $mail->addAddress($email);  
            $mail->isHTML(true);      
            
            if(!$mail->send()) {
                echo "Something went wrong. Please try again.";
            }
            else {
                echo "Email sent successfully.";
            }
            
        } catch (Exception $e) {
            echo "Something went wrong. Please try again.";
        }
        
    }
    
}