【问题标题】:Could not instantiate mail function - Codeigniter and PHPMailer无法实例化邮件功能 - Codeigniter 和 PHPMailer
【发布时间】:2018-09-18 15:14:17
【问题描述】:

我想使用 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,因为我不需要它.. 我希望你能帮助我:)

【问题讨论】:

  • 请记住,PHP 具有调试模式,因此您可以查看故障发生的确切位置。但是这个主机可能无法发送邮件。但可以肯定的是,您必须对此进行更多调试。
  • 这个错误通常意味着你没有安装本地邮件服务器,就像 PHPMailer 文档说的那样。

标签: php codeigniter phpmailer


【解决方案1】:

您没有包含您的 PHPMailer 配置。既然你不使用 SMTP,你有这个设置吗?

$mail->isSendmail();

另外,假设您使用的是 CI3,如果您使用 Composer 安装 PHPMailer 并让它自动加载,可能会更容易。

我刚刚对此进行了测试,使用 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;
        }
    }
}

【讨论】:

  • isMail 是默认的,所以你不需要调用它。
  • 呃,不。不要那样做,它更不安全、更慢,而且你忽略了使它正常工作所需的其他事情。
  • $mail->isSendmail() 对我不起作用。它给了我错误“无法执行:/usr/sbin/sendmail”。
  • 我认为您的问题与 CI 或 PHPMailer 无关。第一个错误是说它不能实例化 PHP 的邮件函数。关于 sendmail 的第二个问题可能是您的 sendmail 路径与默认路径不同。我的建议是将 CI 和 PHPMailer 排除在外,并首先找出 PHP 的邮件功能有什么问题。查看secure.php.net/manual/en/function.mail.php 的示例并尝试发送测试电子邮件。
猜你喜欢
  • 2015-01-17
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
相关资源
最近更新 更多