How to integrate PHPMailer with Codeigniter 3

Hi, I am trying to use the PHPMailer library from GitHUB in my Codeigniter application.

I downloaded the code and unzipped it in a folder application\library. So I have a folder called vendor, inside which is the source code of PHPMailer.

Now I created a file with the name Bizmailer_Controller.php.

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

/**
* 
*/
class Bizmailer_Controller extends CI_Controller
{

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

        require "vendor\phpmailer\phpmailer\PHPMailerAutoload";
        $this->Bizmailer = new PHPMailer();
        //Set the required config parameters
        $this->Bizmailer->isSMTP();                                      // Set mailer to use SMTP
        $this->Bizmailer->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
        $this->Bizmailer->SMTPAuth = true;                               // Enable SMTP authentication
        $this->Bizmailer->Username = 'user@example.com';                 // SMTP username
        $this->Bizmailer->Password = 'secret';                           // SMTP password
        $this->Bizmailer->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
        $this->Bizmailer->Port = 465;    
        //return $api;
    }
}

Now in my controllers I am trying to load it like this:

$this->load->library('Bizmailer');
$mail = new Bizmailer();

And I get this error:

An error has been detected.

Unable to load requested class: Bizmailer

Therefore, please explain to me how I can download or integrate this library in Codeigniter.

+8
source share
3 answers

1. PHP Mailer

PHPMailer Build Github.

" " zip - . PHP Mailer Download as ZIP

zip PHPMailer-master. application/third_party/ phpmailer. - enter image description here

2. PHP Mailer Library

Imho , PHPMailer (Phpmailer_library.php)

class Phpmailer_library
{
    public function __construct()
    {
        log_message('Debug', 'PHPMailer class is loaded.');
    }

    public function load()
    {
        require_once(APPPATH."third_party/phpmailer/PHPMailerAutoload.php");
        $objMail = new PHPMailer;
        return $objMail;
    }
}

3. , ..

class Welcome extends CI_Controller {


    public function index()
    {
        $this->load->library("phpmailer_library");
        $objMail = $this->phpmailer_library->load();
    }
}

, . - , ;)


25.06.2018

, PHPMailer , :

1.)

, - Codeigniter Composer - - config.php

$config['composer_autoload'] = true;

-

composer require phpmailer/phpmailer

application/vendor phpmailer phpmailer.

class Phpmailer_library
{
    public function __construct()
    {
        log_message('Debug', 'PHPMailer class is loaded.');
    }

    public function load()
    {
        $objMail = new PHPMailer\PHPMailer\PHPMailer();
        return $objMail;
    }
}

2.)

1

class Phpmailer_library
{
    public function __construct()
    {
        log_message('Debug', 'PHPMailer class is loaded.');
    }

    public function load()
    {
        require_once(APPPATH.'third_party/phpmailer/src/PHPMailer.php');
        require_once(APPPATH.'third_party/phpmailer/src/SMTP.php');

        $objMail = new PHPMailer\PHPMailer\PHPMailer();
        return $objMail;
    }
}

+14

Codeigniter 2/3 php mailer, .

0

**

2019

**

PHPMailer application/third_party/ CodeIgniter.

(application/library/Phpmailer_lib.php) PHPMailer.

  • PHPMailer.
  • PHPMailer.
  • PHPMailer.

    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(true);
        return $mail;
    }
    }
    

SMTP- PHPMailer , .

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';
        }
    }

}
0

Source: https://habr.com/ru/post/1680484/


All Articles