0

This is driving me nuts.

Downloaded and installed ZendFramework. Trying to send a DKIM signed mail but it is giving me this error(which i copied directly off their documentation).

Error: Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\Project\send_form_email.php:12 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Project\send_form_email.php on line 12

// sign message with dkim
$signer = $this->getServiceLocator()->get('DkimSigner');
$signer->signMessage($mail);

What am I doing wrong here?

Edit: Added all coding

<?php

require 'vendor/autoload.php';

$mail = new \Zend\Mail\Message();
$mail->setBody("Test world!");
$mail->setFrom('noreplytest@sanscalc.co.za');
$mail->addTo('jr.swart49995@gmail.com');
$mail->setSubject('le subject');

// sign message with dkim
$signer = $this->getServiceLocator()->get('DkimSigner');
$signer->signMessage($mail);

// send message
$transport = new \Zend\Mail\Transport\Sendmail();
$transport->send($mail);
?>

Thank you for contacting us. We will be in touch with you very soon.

<?php

}
?>
  • so which error? – MrSmile Jul 20 '18 at 06:25
  • The code you have posted is insufficient. The error message suggests you are either running this code from inside a static method, or not inside a method at all. – zenzelezz Jul 20 '18 at 09:44
  • `$this` is a **reference** to the current object. See more [in this answer](https://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php) – rkeet Jul 25 '18 at 12:16

1 Answers1

4

$this is only valid when called from inside an object. Your code is not within an object, which is why calling $this throws the error.

$this->getServiceLocator()->get('DkimSigner');

your code is expecting to call a getServiceLocator() method defined within an object, which you have not done as your code is more procedural in nature.

I am guessing you're trying to use the example code from here: https://github.com/joepsyko/zf-dkim

If you check that page it tells you the code should be within a controller (object) where $this is in a valid context, and also where the getServiceLocator method is defined.

try and Install the standard "getting started" zend framework and try the snippet within a controller (https://docs.zendframework.com/tutorials/getting-started/skeleton-application/)

or do away with the use of the service locator in your code and instantiate the DKimm Signer yourself within your script manually.

Andrew
  • 12,617
  • 1
  • 34
  • 48