Search code examples
phpcodeigniterphpmailer

Could not instantiate mail function - Codeigniter and PHPMailer


I want to send an email using PHPMailer and I'm using Codeigniter

public function check_email(){
        $response = array('error' => false);
        $email = $this->input->post('email');
        $check_email = $this->fp_m->check_email($email);

        if($check_email){
            $this->load->library('phpmailer_library');
            $mail = $this->phpmailer_library->load();

            $mail->setFrom('[email protected]', 'Mailer');                
            $mail->addAddress($email);
            $mail->isHTML(true);
            $mail->Subject = "Reset Password";
            $mail->Body = "
                Hi,<br><br>

                In order to reset your password, please click on the link below:<br>
                <a href='
                http://example.com/resetPassword.php?email=$email
                '>http://example.com/resetPassword.php?email=$email</a><br><br>

                Kind Regards,<br>
                Kokushime
            ";
            if($mail->send()){
                $response['error'] = false;
                $response['message'] = "The Email Sent. Please Chect Your Inbox";
            }else{
                $response['error'] = true;
                $response['message'] = "There's Something wrong while sending the message". $mail->ErrorInfo;
                ;
            }
        }else{
            $response['error'] = true;
            $response['message'] = 'The email that you entered is not associated with admin account';
        }
        echo json_encode($response);
    }

But it gives me error Could not instantiate mail function. BTW, I'm not using SMTP because i don't need that.. I hope that you can help me :)


Solution

  • You did not include your PHPMailer config. Since you are not using SMTP do you have this set?

    $mail->isSendmail();
    

    Also, assuming you are using CI3 it would probably be easier if you installed PHPMailer with composer and had it autoload.

    I just tested this and it works fine using sendmail.

    <?php
    if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    class Phpmailer_test extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function index()
        {
            $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
            try {
                //Server settings
                $mail->isSendmail();                                
    
                //Recipients
                $mail->setFrom('[email protected]', 'Mailer');
                $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
                $mail->addReplyTo('[email protected]', 'Information');
    
                //Content
                $mail->isHTML(true);                                  // Set email format to HTML
                $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;
            }
        }
    }