0

hi i have problem with namespace in codeIgniter.

what i'm trying to do is:

  1. i have downloaded phpmailer with composer

  2. it has 2 namespaces they are

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

whenerver i include this namespace it will give this error

Class 'Frontend_Controller' not found

here is my controller code :

No Error Code - this code works fine

class Welcome extends Frontend_Controller {
   //my code goes here
}

This code gives Error

 require "vendor/autoload.php";

 use PHPMailer\PHPMailer\PHPMailer;

 use PHPMailer\PHPMailer\Exception;

class Welcome extends Frontend_Controller {
   // my code goes here....
}

i have tried this link but giving same error : https://gist.github.com/JeyKeu/7533af3b9b5fd078910d if i put the code in application\config\config.php

please help me thanks in advance

2 Answers2

0

inside config folder create a file email.php...

<?php
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; 
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'youraccount@gmail.com'; 
    $config['smtp_pass'] = 'test@12#'; 
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['newline'] = "\r\n";
?>

and in main controller...

    $params['mailtype'] = 'html';
    $params['subject'] = ' Something ';
    $this->email->set_mailtype("html");
    $this->email->from('info@test.co.in', 'Application name');
    $this->email->to('example@gmail.com');
    $this->email->subject($params['subject']);
    $this->email->message($this->load->view('your_view_page', $params, true));
    $this->email->send();
Mahesh
  • 93
  • 9
0

CodeIgniter 2.x

add the below line in index.php

require "vendor/autoload.php";

and now inside the controller

use PHPMailer\PHPMailer\PHPMailer;

for more info Ref : https://stackoverflow.com/a/15244577/7296317

CodeIgniter 3.x

If you want CodeIgniter to use a Composer auto-loader, just set $config['composer_autoload'] to TRUE or a custom path in application/config/config.php.

REF : https://www.codeigniter.com/user_guide/general/autoloader.html

and also at the top of your file where you want to use PHP mailer, probably need something like:

use PHPMailer;
Dinesh Kumar
  • 649
  • 3
  • 8