链接phpmailer时出错

Fault when linking phpmailer

在最近的项目中,有人要求我 link 重设密码表单,以使用 SMTP php 邮件程序从 amazonAWS 自动发送邮件。我已将调用代码 (mail->*) 放入一个函数中。然而,自从这样做以来,我似乎收到了 "Fatal error: Call to undefined method smtp::connected() in /var/www/html/PHPMailer-master/class.phpmailer.php on line 1530"。我似乎无法找到发生这种情况的原因,想知道是否有人看到过类似的回复。

如有任何帮助,我们将不胜感激。代码如下

smtp.php:

    require_once('/var/www/html/PHPMailer-master/PHPMailerAutoload.php');
    class smtp {

        var $mail;

        function __construct () {
            $this->mail = new PHPMailer;
            $this->mail->isSMTP();
            $this->mail->Host = 'email-smtp.eu-west-1.amazonaws.com';
            $this->mail->SMTPAuth = true;
            $this->mail->Username = '---';
            $this->mail->Password = '---';
            $this->mail->SMTPSecure = 'tls';
            $this->mail->SMTPKeepAlive = true;
            $this->mail->Port = 587;
            $this->mail->From = '---.com';
            $this->mail->FromName = '---';
            $this->mail->WordWrap = 50;
            $this->mail->isHTML(true);
            $this->mail->AltBody = 'Please use an HTML-enabled email client to view this message.';
        }

        function setSubject ($subject) {
            $this->mail->Subject = $subject;
        }

        function setBody ($body) {
            $this->mail->Body = stripslashes($body);
        }

        function sendTo ($to) {
            $this->mail->clearAddresses();
            $this->mail->addAddress($to);

            if (!$this->mail->send()) {
                return false;
            } else {
                return true;
            }
        }

    }

调用函数:

public function resetlink(){
        if($this->_passed){
            $pass = $this->_hash->getrand();
            if($this->_db->userValidate($_POST['email'])->getValidate()){
                $this->_db->update('---', array(
                        'username' => $_POST['email'], 
                        'value' => $pass

                ));

                $smtp = new smtp();
                $smtp->setSubject('sample subject');
                $smtp->setBody('sample body');
                $smtp->sendTo($_POST['email']);


            }else{
                echo "invalid email";
            }
        } return $this;
    }

导致问题的函数在 class.phpmailer.php:

if ($this->smtp->connected()) {
            return true;
        } 

当程序代码为 运行 时,能够发送测试电子邮件,因此 mail->* 变量是正确的。但是自从将代码作为函数放入 运行 后,就发现了一个错误。再次感谢您的帮助。

谢谢

PHPMailer 包含并使用 class named SMTP

如果你的 class smtp 在它之前定义,PHPMailer 可能会实例化你的 SMTP class 而不是它自己的,其中函数没有定义。

你应该给你自己的 class 一个不同的名字,这样他们就不会发生冲突。