1
votes

In a recent project, I've been asked to link a reset password form to automatically send mail from an amazonAWS using SMTP php mailer. I've placed the calling code (mail->*) into a function. However since doing this, I seem to be receiving a "Fatal error: Call to undefined method smtp::connected() in /var/www/html/PHPMailer-master/class.phpmailer.php on line 1530". I can't seem to find why this is happening and was wondering if anyone has seen a similar response.

Any help would be appreciated. The code is as follows

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;
            }
        }

    }

calling function:

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;
    }

The function causing the issue is in class.phpmailer.php:

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

A test email was able to send when the code was run in procedural style, so the mail->* variables are correct. However ever since the code was put to run as a function, an error was found. Once again, any help would be appreciated.

Thanks

1
If PHPMailer uses a class named SMTP. You should give your own class a different name so they can't collide. - Gerald Schneider
i can't believe i did't even look at this. Fixed the problem. Thanks - Juakali92
The forthcoming PHPMailer 5.4 adds namespaces so this will no longer be a problem. - Synchro

1 Answers

1
votes

PHPMailer contains and uses a class named SMTP.

If your class smtp is defined before it, it is possible that PHPMailer instanciates your SMTP class instead of its own, where the function is not defined.

You should give your own class a different name so they can't collide.