SMTP emails in CodeIgniter In this example, we will discuss how to send SMTP emails in CodeIgniter


PHPMailer Installation 

installation via Composer is the recommended way to install PHPMailer.

composer require phpmailer/phpmailer


controllers/Test.php



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

// Load Composer's autoloader
require 'vendor/autoload.php';

use PHPMailerPHPMailerPHPMailer;

class Test extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function send_mail() {

        $mail = new PHPMailer();
        $mail->IsSMTP(); /* we are going to use SMTP */
        $mail->SMTPAuth = true; /* enabled SMTP authentication */
        $mail->SMTPSecure = "ssl";  /* prefix for secure protocol to connect to the server */
        $mail->Host = "";    //host name  /* if you are using gmail put : smtp.gmail.com */
        $mail->Port = 465;                   /* SMTP port to connect to GMail */
        $mail->Username = "";  /* user email address */
        $mail->Password = "";            /* password in GMail */
        $mail->SetFrom('', 'Mail');  /* Who is sending */
        $mail->isHTML(true);
        $mail->Subject = "Mail Subject";
        $mail->Body = '
            
            
                Title
            
            
            Heading
            Message Body
            With Regards
            Your Name
            
            
        ';
        $destino = "receiver@mail.com"; // Who is addressed the email to
        $mail->AddAddress($destino, "Receiver");
        if (!$mail->Send()) {
            return false;
        } else {
            return true;
        }
    }

}


Thank you for visiting us!!