Rajan
Rajan

Reputation: 2425

How to integrate PHPMailer with Codeigniter 3

Hi I am trying to use PHPMailer Library from GitHUB in my Codeigniter application.

I downloaded the code and unzipped in my application\library folder. So there I have a folder called vendor inside which resides the source code for PHPMailer.

Now I created a File named 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 = '[email protected]';                 // 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 Was Encountered

Unable to load the requested class: Bizmailer

So Please guide me how I can load or integrate this library in Codeigniter.

Upvotes: 10

Views: 52241

Answers (4)

Ahmad Pujianto
Ahmad Pujianto

Reputation: 359

What about putting it on helper?

First you need to have composer installed in your local. If not then google how to install it.

Then get phpmailer using composer by opening cmd then make a folder for PHPMailer files. Then type command : composer require phpmailer/phpmailer

Here my code :

  1. Copy all files in folder "vendor" in your phpmailer folder to your web app in folder "/system/application/vendor/"

  2. Open one of your helper file (or create new helper file) in "/system/application/helper/"

  3. Use this code

    function sendemail($to,$to_name,$subject,$message){ 
        require_once APPPATH.'vendor/phpmailer/phpmailer/src/Exception.php';
        require_once APPPATH.'vendor/phpmailer/phpmailer/src/PHPMailer.php';
        require_once APPPATH.'vendor/phpmailer/phpmailer/src/SMTP.php';
    
        //Make instance of PHPMailer object
        $mail = new PHPMailer\PHPMailer\PHPMailer();  
    
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host     = 'mail.yoursite.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail->Password = 'yourpassword';
        $mail->SMTPSecure = 'ssl';  
        $mail->Port     = 465;
    
        $mail->setFrom('[email protected]', 'your name');
        //$mail->addReplyTo('[email protected]', 'CodexWorld');
    
        // Add a recipient
        $mail->addAddress($to); //,$to_name
    
    
        // Add cc or bcc 
        $mail->addCC('[email protected]', 'your name');
    
        // Set email format to HTML
        $mail->isHTML(true);
    
        // Email subject
        $mail->Subject = $subject; 
    
        // Email body content
        $mailContent = $message."<br/><br/>
            <p>Auto notification *** <br/>DO NOT REPLY TO THIS ADDRESS as it is not attended.</p>";
        $mail->Body = $mailContent;
    
        // Send email
        if(!$mail->send()){
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo; die;
        }else{
           //do none or celebrate may be???
        }
    }
    
  4. To call the method, you need to load the helper then call the method like below:

    $this->load->helper('yourhelper');
    sendemail("[email protected]","to name","Subject","Email content ( can use html)");
    

Upvotes: 0

Barungi Stephen
Barungi Stephen

Reputation: 847

**

UPDATE AUGUST 2019

**

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

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.

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

Now send email via SMTP server using PHPMailer from your controller using this code.

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 = '[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
        $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';
        }
    }

}

Upvotes: 8

Atural
Atural

Reputation: 5439

here is a guide

1. installing PHP Mailer

Download the latest PHPMailer Build from Github. You can find the project here

Click now on "clone or download" and download it as zip - as in the image below is shown. PHP Mailer Download as ZIP

The folder in the zip is called PHPMailer-master. Unzip this in your application/third_party/ folder and rename the folder to phpmailer. You should see something like this enter image description here

2. PHP Mailer Library

Imho its best to create a library which handles your PHPMailer Object (Phpmailer_library.php) This library could look like

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. Using this library in one of your controllers, models etc.

class Welcome extends CI_Controller {


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

i think this should pretty much do the job. If you've any troubles, don't hesitate to ask ;)


Update 25.06.2018

Since the PHPMailer guys removed the autoloader you've two options now:

1.) via Composer

for those who didn't know - Codeigniter supports Composer - you simply have to activate the autoload - you can find this in your config.php

$config['composer_autoload'] = true;

For more informations take a look here

After that - run composer like

composer require phpmailer/phpmailer

You now should have within your application/vendor folder the phpmailer files.

The library should look like

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

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

2.) download

follow step 1

The library should look like

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

and everything else should remain the same

Upvotes: 20

MorganFreeFarm
MorganFreeFarm

Reputation: 3733

There is php mailer library for Codeigniter 2/3, you can check this and this.

Upvotes: 0

Related Questions