0

i am trying to send email using PHP mailer in codeigniter. Below is my code

below 3 files are added to /project/application/third_party

  1. PHPMailer.php
  2. Exception.php
  3. SMTP.php

in my /project/application/libraries i have below

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

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Mailer{
    public function __construct(){
        log_message('Debug', 'PHPMailer class is loaded.');
    }

    public function load(){
        // Include PHPMailer library files
        require_once APPPATH.'third_party/Exception.php';
        include APPPATH.'third_party/PHPMailer.php';
        require_once APPPATH.'third_party/SMTP.php';

        $mail = new PHPMailer(true);
        return $mail;
    }
}

In my Controller :

class Email extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('Mailer');
    }
    function sendemail(){
       // echo APPPATH.'third_party/PHPMailer.php';
        $mail = $this->mailer->load();             
        try {
            $mail->isSMTP();                                            // Send using SMTP
            $mail->Host       = '*****'; 
            $mail->SMTPAuth   = true;    
            $mail->Username   = '*****';                     // SMTP username
            $mail->Password   = 'secret';                               // SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; 
            $mail->Port       = 587;  
            $mail->setFrom('email@email.com', 'Reliet');
            $mail->addAddress('receivere@gmail.com', 'John');     
            $mail->addReplyTo('infom@email.com', 'Information');   

            $mail->isHTML(true);                                   
            $mail->Subject = 'Here is the subject';
            $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }

Below is the error

An uncaught Exception was encountered Type: Error

Message: Class 'PHPMailer' not found

Please guide me where am i doing wrong

  • Move the `require_once` and `include` statements to before the `use`. It would be better to setup autoloading though (if you use `composer` to install libraries this will be handled for you) – Matt Nov 27 '20 at 08:47
  • that did not work , to move require statements before the use statements – Sikander Nawaz Nov 27 '20 at 08:52
  • Ok at the top of PHPMailer.php does it say "namespace", what does that line say? – Matt Nov 27 '20 at 08:56
  • `use` statements should typically be before any includes/requires. They merely define aliases; the classes do not have to exist at that point. Are you sure this is the right way to load packages with CodeIgniter? If so, it's horrible! See [this question](https://stackoverflow.com/questions/13741917/how-to-use-composer-packages-in-codeigniter) for how to load packages using composer in CodeIgniter, so that you don't have to deal with any of that yucky manual `load->library` stuff! – Synchro Nov 27 '20 at 09:14
  • Hey, did you solved it? – Syafiqur__ May 11 '21 at 03:25

0 Answers0