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!