Anoop Babu
Anoop Babu

Reputation: 188

Call to a member function has() on a non-object on send mail in swifmailer

i have created a service

services:
app.EmailAndSms:
    class: AppBundle\PublicFunctions\EmailAndSms
    arguments: ["%parameter1%","%parameter2%"]

and

namespace AppBundle\PublicFunctions;
use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class EmailAndSms extends Controller{

public function __construct($parameter1,$parameter2) {
  .....
  ....
}

public static function sendEMail() {

$Con=  new Controller;



        $message = \Swift_Message::newInstance()
                ->setSubject($maildata['sub'])                  
                ->setFrom('[email protected]')
                ->setTo($maildata['To'])                       
                ->setReturnPath('[email protected]')               ->setBody($Con->renderView(                                          'Emails/EMailTemplate.html.twig', array('content'   => $Passtemplate)), 'text/html');

      }

       }

got error Error: Call to a member function has() on a non-object

"file": "/var/www/html/xxxx_rest/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php", "line": 162,

Upvotes: 2

Views: 724

Answers (1)

Mawcel
Mawcel

Reputation: 2007

You should start by cleaning up your code.

Remove the static modifier, static methods are to be avoided in general.

You don't need the new Controller instance since you already are extending the controller class so instead of

$Con->renderView('Emails/EMailTemplate.html.twig', array('content' => $Passtemplate)), 'text/html');

Just do

$this->renderView('Emails/EMailTemplate.html.twig', array('content' => $Passtemplate)), 'text/html');

the non-object has() is called on is the service container of your controller because as you instantiate your controller yourself the container is not injected.

In the end you don't need to exten Controller either, you should just get the twig service since this is what you need and not the whole service container.

To fix all this inject twig in your service as well as swiftmailer to send your email:

 services:
    app.EmailAndSms:
        class: AppBundle\PublicFunctions\EmailAndSms
       arguments: ["%parameter1%","%parameter2%", '@twig', @mailer]

Then in your class:

namespace AppBundle\PublicFunctions;

class EmailAndSms {

  private $twig;
  private $mailer;

  public function __construct($parameter1,$parameter2, \Twig_environment $twig, $mailer) {
    .....
    ....
    $this->twig = $twig;
    $this->mailer = $mailer;
  }

  public function sendEMail($maildata) { 

        $message = \Swift_Message::newInstance()
                ->setSubject($maildata['sub'])                  
                ->setFrom('[email protected]')
                ->setTo($maildata['To'])                       
                ->setReturnPath('[email protected]')
                ->setBody($this->twig->render('Emails/EMailTemplate.html.twig', array('content'   => $Passtemplate)));

        $success = $this->mailer->send($message);

        return $success;
   }

}

Now to use this service from a controller :

$this->get('app.EmailAndSms')->sendEmail($maildata);

Upvotes: 2

Related Questions