簡體   English   中英

無法實例化郵件功能-Codeigniter和PHPMailer

[英]Could not instantiate mail function - Codeigniter and 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);
    }

但是它給我錯誤無法實例化郵件功能 順便說一句,我不使用SMTP,因為我不需要它。.希望您能為我提供幫助:)

您沒有包括PHPMailer配置。 由於您未使用SMTP,因此您有此設置嗎?

$mail->isSendmail();

另外,假設您使用的是CI3,則將PHPMailer與composer一起安裝並自動加載可能會更容易。

我剛剛進行了測試,使用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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM