2

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('from@example.com', '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 :)

  • 1
    Remember that PHP has a debug mode so you can see where exactly the failure occurs. But this host probably can't send mails. But for sure you have to debug into this a bit more. –  Sep 18 '18 at 16:21
  • This error usually means you don't have a local mail server installed, just as the PHPMailer docs say. – Synchro Sep 18 '18 at 20:07

1 Answers1

0

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('from@example.com', 'Mailer');
            $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
            $mail->addReplyTo('info@example.com', '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;
        }
    }
}
Tom Vaughan
  • 390
  • 2
  • 16
  • `isMail` is the default, so you don't need to call it. – Synchro Sep 18 '18 at 20:08
  • Uh, no. Don’t do that, it’s less safe, slower, and you’ve omitted the other things you need to make it work properly. – Synchro Sep 19 '18 at 05:52
  • $mail->isSendmail() is not working for me. it gives me error ''Could not execute: /usr/sbin/sendmail". –  Sep 19 '18 at 14:19
  • I don't think your problem is with CI or PHPMailer. The fist error is saying it cannot instantiate PHP's mail function. The second one about sendmail could be that your path to sendmail is different than the default. My suggestion would be to take CI and PHPMailer out of the equation and find out what is wrong with PHP's mail function first. Look at the examples at https://secure.php.net/manual/en/function.mail.php and try to send a test email. – Tom Vaughan Sep 20 '18 at 15:19