Send Email using PHPMailer in CodeIgniter

CodeIgniter’s Email class is the simplest way to send email in CodeIgniter application. Not only the text/html email but also you can send the email via SMTP server using CodeIgniter Email library. This Email class is a system library and comes with the CodeIgniter framework. So, it can be easily used in the CodeIgniter application without an additional library. Alternatively, PHPMailer library is the best option to send email via SMTP server without using the CodeIgniter default Email library.

Generally, PHPMailer library is used to send email with SMTP server in PHP. You can also use the PHPMailer library in CodeIgniter framework to send email using SMTP server. In this tutorial, we will show you how to integrate PHPMailer in CodeIgniter 3 application and send email via SMTP server using PHPMailer in CodeIgniter.

Integrate PHPMailer in CodeIgniter

At first, download the latest PHPMailer library files and place all the files in the application/third_party/ folder of your CodeIgniter application.

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

Note that: All the PHPMailer library files are included in the source code, you don’t need to download PHPMailer separately.

Now, create a library (application/libraries/Phpmailer_lib.php) to handle the PHPMailer object.

  • Include the PHPMailer library files.
  • Initialize the PHPMailer class.
  • Return the PHPMailer object.
<?php
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

Using PHPMailer_Lib library you can send email with PHPMailer in your CodeIgniter application. The following example code shows how to send email via SMTP server using PHPMailer from the controller of the CodeIgniter application.

  • Load the PHPMailer_Lib library.
  • Call the load() function of PHPMailer_Lib.
  • Specify the host ($mail->Host), username ($mail->Username), password ($mail->Password), and port ($mail->Port) as per your SMTP server credentials.
  • Set isHTML() to TRUE for sending HTML email.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Email extends CI_Controller{
    
    function  __construct(){
        parent::__construct();
    }
    
    function send(){
        // Load PHPMailer library
        $this->load->library('phpmailer_lib');
        
        // PHPMailer object
        $mail $this->phpmailer_lib->load();
        
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host     'smtp.example.com';
        $mail->SMTPAuth true;
        $mail->Username 'user@example.com';
        $mail->Password '********';
        $mail->SMTPSecure 'ssl';
        $mail->Port     465;
        
        $mail->setFrom('info@example.com''CodexWorld');
        $mail->addReplyTo('info@example.com''CodexWorld');
        
        // Add a recipient
        $mail->addAddress('john.doe@gmail.com');
        
        // Add cc or bcc 
        $mail->addCC('cc@example.com');
        $mail->addBCC('bcc@example.com');
        
        // Email subject
        $mail->Subject 'Send Email via SMTP using PHPMailer in CodeIgniter';
        
        // Set email format to HTML
        $mail->isHTML(true);
        
        // 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
        if(!$mail->send()){
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' $mail->ErrorInfo;
        }else{
            echo 'Message has been sent';
        }
    }
    
}

Send Email with Gmail SMTP using PHPMailer in CodeIgniter

You can use Gmail SMTP to sending email with PHPMailer in CodeIgniter. Before getting started with Gmail SMTP, some changes are needed in your Google account settings to use Gmail SMTP.

  • Login to your Google account and go to the My Account page.
  • Click the Signing in to Google link under the Sign-in & security section.
    send-email-php-gmail-smtp-account-settings-codexworld
  • Scroll down to Password & sign-in method section and turn Off the 2-Step Verification.
    send-email-php-gmail-smtp-off-2-step-verification-codexworld
  • Scroll down to Apps with account access section and turn on Allow less secure apps.
    send-email-php-gmail-smtp-allow-less-secure-apps-codexworld

You are done! Now Google will allow you to use Gmail SMTP for sending email from the PHP script of your CodeIgniter application.

Specify your Gmail account’s email address as username ($mail->Username), password ($mail->Password), SMTP host and port.

// SMTP configuration
$mail->isSMTP();
$mail->Host 'smtp.gmail.com';
$mail->SMTPAuth true;
$mail->Username 'codexworld@gmail.com';
$mail->Password '********';
$mail->SMTPSecure 'tls';
$mail->Port 587;

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

1 Comment

  1. Sohail Said...

Leave a reply

keyboard_double_arrow_up