-1

I don't use classes too often but have been to try and make my code easier to manage. I'm trying to extend PHPMailer and then create the object and send the mail however am having issues using $this inside functions within the extending class.

require 'phpmailer/PHPMailerAutoload.php';

class Mailz extends PHPMailer {
    public $From     = 'noreply@example.com';
    public $FromName = 'Examplemail';
    public $Host     = 'mail.example.com';
    public $SMTPAuth = true;
    public $Username = 'noreply@example.com';
    public $Password = '123example';
    public $SMTPSecure = 'ssl';
    public $Port = 465;
    public $Priority    = 1;
    public $CharSet     = 'UTF-8';
    public $Encoding    = '8bit';
    public $Content = 'text/html';

    public function send() {
        return parent::send();
    }

/*
 * welcome email
 */

public static function sendWelcome($email,$password) {

   $message = str_replace(
        array(
        '[body]',
          '[first_name]',
          '[email_address]',
          '[password]'

        ),
        array(
        $body = 'Lorem ipsum yadadada',
        $email_address = $email,
        $password,
        ),
        file_get_contents('welcome.html')
    );

    $this->Subject = 'Subject Line!';
    $this->Body    = $message;

    if(!$this->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $this->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }


    }
}


$sendemail = new Mailz();
$sendemail->sendWelcome('recipient@gmail.com','P4$$w0rd');

I've been trying to fix it for a few hours now with different ways but am no closer to finding a solution. The error I am currently getting is: Uncaught Error: Using $this when not in object context on this line - $this->Subject = 'Subject Line!'; and the same would probably happen to the other uses of $this.

What am I missing!

Erbilacx
  • 209
  • 2
  • 9

3 Answers3

1

Your function is static. $this isn't set in a static function.

Remove the static keyword from your sendWelcome function and it should work fine

TommyBs
  • 9,354
  • 4
  • 34
  • 65
  • Got it and now have a better understanding of static functions. I hadn't been using them inside classes and obviously moving this from my normal functions file into the class helped to create this issue. Working perfectly now, thanks! – Erbilacx Jul 03 '17 at 13:58
1

You try to use the $this keyword within a static function, but you don't have an instance of your class to refer to with $this when in static context, so you will get an error.

Tobias F.
  • 1,050
  • 1
  • 11
  • 23
  • 1
    Got it and now have a better understanding of static functions. I hadn't been using them inside classes and obviously moving this from my normal functions file into the class helped to create this issue. Working perfectly now, thanks! – Erbilacx Jul 03 '17 at 13:58
0

You can not use $this in static methods, however if want to use static try making an object of self.

$mailer = new self();
$mailer->Subject = '';

This way you can call your class without instanciating if like.

Mailz::sendWelcome(<subject>, <password>);

Or you can simply remove static from method declaration.

anwerj
  • 2,386
  • 2
  • 17
  • 36