I am writing an Custom Symfony Service, a Mailer, to send all of my mails. Therefore I am trying to inject the mailer and the template Service inside the custom Service Class. I already tried other solutions from stackoverflow, like e.g. 10304468, but this didn't helped me.
This is my Mailer Service Class
namespace Acme\DemoBundle\Mail;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Swift_Message;
class Mailer {
protected $mailer;
protected $templating;
public function __construct(Swift_Message $mailer, EngineInterface $templating) {
$this->mailer = $mailer;
$this->templating = $templating;
}
public function NewUserMail($newuser){
$message = new Swift_Message();
$message->newInstance()
->setSubject('Demo Subject')
->setFrom(array('info@example.com' => 'Your Company'))
->setTo($newuser->getEmail())
->setBody($this->templating->render('AcmeDemoBundle:Mail:newuser.html.twig'), 'text/html');
$this->sendMail($message);
}
public function sendMail($message) {
$this->mailer->send($message);
}
}
This is my Service.yml (which gets loaded through the Dependecy Injection Extension file:
acme_demo_bundle.mailer:
class: Acme\DemoBundle\Mail\Mailer
arguments: ["@mailer", "@templating"]
Now when I am calling the Mailer Service inside a Controller, I get this Error:
Catchable Fatal Error: Argument 1 passed to Acme\DemoBundle\Mail\Mailer::__construct() must be an instance of Swift_Message, none given,
How I call the Mailer Service inside the Controller:
/...
$message = new Mailer();
$message->NewUserMail($newuser);
/...
If I insert the mailer and templating service from the container, when generating the new Mailer Class, i won't get the error. But I thought, the Service.yml is insertig the services into my class, but obviously is does not.
I also tried this inside another symfony project, same problem. It looks like I am missing something here.... :(