How to Send Email using PHPMailer in CodeIgniter

How to Send Email using PHPMailer in CodeIgniter

How to Send Email using PHPMailer in CodeIgniter

0 Sales


The Email class in CodeIgniter is the most basic way to send email in a CodeIgniter application. Not only can you send text/html emails, but you can also use the CodeIgniter Email library to send emails using SMTP servers. This Email class is part of the CodeIgniter framework's system library. As a result, it may be readily utilised in the CodeIgniter application without the need for an external library. Instead than utilising the CodeIgniter default Email library, the PHPMailer library is the best alternative for sending email over SMTP server.

In general, the PHPMailer library is used in PHP to send email via an SMTP server. To send email using an SMTP server, you may also utilise the PHPMailer module in the CodeIgniter framework. In this article, we'll teach you how to integrate PHPMailer into a CodeIgniter 3 application and use PHPMailer to send email over an SMTP server.

Integrate PHPMailer in CodeIgniter

To begin, grab the most recent PHPMailer library files and insert them all in your CodeIgniter application's application/third party/ folder.

└── third_party/
    └── PHPMailer/
        ├── Exception.php
        ├── OAuth.php
        ├── PHPMailer.php
        ├── POP3.php
        └── SMTP.php

Create a library to handle the PHPMailer object (application/libraries/Phpmailer lib.php).

 - Include the PHPMailer library files in your project.
 - Set up the PHPMailer class.
 - The PHPMailer object is returned.

defined('BASEPATH') OR exit('No direct script access allowed');

 * CodeIgniter PHPMailer Class
 * This class enables SMTP email with PHPMailer
 * @category    Libraries
 * @author      CodexWorld
 * @link        https://www.codexworld.com

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class PHPMailer_Lib
    public function __construct(){
        log_message('Debug''PHPMailer class is loaded.');

    public function load(){
        // Include PHPMailer library files
        require_once APPPATH.'third_party/PHPMailer/Exception.php';
        require_once APPPATH.'third_party/PHPMailer/PHPMailer.php';
        require_once APPPATH.'third_party/PHPMailer/SMTP.php';
        $mail = new PHPMailer;
        return $mail;

Send Email using PHPMailer in CodeIgniter

You may send email using PHPMailer in your CodeIgniter application by using the PHPMailer Lib module. The following example code demonstrates how to send email through SMTP server using PHPMailer from the CodeIgniter application's controller.

 - Load the PHPMailer Lib library into memory.
 - Invoke PHPMailer Lib's load() method.
 - As per your SMTP server credentials, enter the host ($mail->Host), username ($mail->Username), password ($mail->Password), and port ($mail->Port).
 - To send HTML email, set isHTML() to TRUE.

defined('BASEPATH') OR exit('No direct script access allowed');

class Email extends CI_Controller{
    function  __construct(){
    function send(){
        // Load PHPMailer library
        // PHPMailer object
        $mail $this->phpmailer_lib->load();
        // SMTP configuration
        $mail->Host     'smtp.example.com';
        $mail->SMTPAuth true;
        $mail->Username '[email protected]';
        $mail->Password '********';
        $mail->SMTPSecure 'ssl';
        $mail->Port     465;
        $mail->setFrom('[email protected]''CodexWorld');
        $mail->addReplyTo('[email protected]''CodexWorld');
        // Add a recipient
        $mail->addAddress('[email protected]');
        // Add cc or bcc 
        $mail->addCC('[email protected]');
        $mail->addBCC('[email protected]');
        // Email subject
        $mail->Subject 'Send Email via SMTP using PHPMailer in CodeIgniter';
        // Set email format to HTML
        // Email body content
        $mailContent "<h1>Send HTML Email using SMTP in CodeIgniter</h1>
            <p>This is a test email sending using SMTP mail server with PHPMailer.</p>";
        $mail->Body $mailContent;
        // Send email
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' $mail->ErrorInfo;
            echo 'Message has been sent';

Send Email with Gmail SMTP using PHPMailer in CodeIgniter


1. To send emails with PHPMailer in CodeIgniter, you may utilise Gmail SMTP. Before you can utilise Gmail SMTP, you must to make certain adjustments to your Google account settings.

2. Go to the My Account page after logging into your Google account.
Under the Sign-in & security area, click the Signing in to Google link.

3. Scroll down to the Password & Sign-in Method section and disable 2-Step Verification.
4. Scroll down to the section Apps with account access and enable Allow less secure apps.

You're finished! Google will now allow you to utilise Gmail SMTP to send emails from your CodeIgniter application's PHP script.

Enter the email address associated with your Gmail account as the username ($mail->Username), password ($mail->Password), SMTP host, and port.


You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : Feb 17, 2022

Updated Date : Feb 17, 2022


Comments : 0

Downloads : 0