无法实例化邮件功能-Codeigniter和PHPMailer

时间:2018-09-18 15:14:17

标签: php codeigniter phpmailer

我想使用PHPMailer发送电子邮件,而我正在使用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);
    }

但是它给了我错误Could not instantiate mail function。 顺便说一句,我不使用SMTP,因为我不需要它。 我希望你能帮助我:)

1 个答案:

答案 0 :(得分: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;
        }
    }
}