1

I'm very new to Symfony and now I have a question about configuring and sending a mail. Actually I'm configuring and sending the mail in the controller, but for me it would be better to configure the mail not in the controller, but in an .ini-file or something else. Therefore I thought the right way would be to configure it as a service, because so I can configure the mail itself in a class and not in the code.

I created a class, that looks like that:

class PwMailer{
protected $mailer;

public function setMailer($mailer)
{
    $this->mailer = $mailer;
}

public function sendEmail($email, $password)
{
    $message = \Swift_Message::newInstance()
        ->setSubject('New Password')
        ->setFrom('xxx@nobody.com')
        ->setTo($email)
        ->setBody('Your new Password is '.$password)
    ;
    $this->mailer->send($message);
}

}

The values $email and $password should come from the controller. In my config-file app\config\config.yml I configured it:

services:
  pw_mailer:
    class:     Pso\LogBundle\PwMailer
    arguments: [sendmail] 

I call the service from controller

$mailer = new Mailer('pw_mailer');
$mailer->send();

Now I got the error "FatalErrorException: Error: Class '...Mailer' not found in '...controller'

My code is a Mix from http://symfony.com/doc/current/book/service_container.html and How can I send emails from a Symfony2 service class?

I would be glad about a hint, if a service container for configuring the mail in a class and not in the controller is the right way and where my mistake in thinking is. Until now I didn't unterstand how the configuration of a service container exactly works.

Greetings

Community
  • 1
  • 1
Micha
  • 43
  • 7
  • `services: pw_mailer:` second line should not be aligned with the first one. – Cheery Nov 15 '14 at 20:01
  • It's a very common misunderstanding. You think that new Mailer will somehow access the container but the two are completely independent. As a starter, you need $mailer = $this->container->get('pw_mailer'); I suspect you will run into a few more issues as well. Might want to get the example in the manual working first then refine. – Cerad Nov 15 '14 at 20:13
  • I tried to begin small. The line $this->mailer->send($message); in the class actually causes an error "FatalErrorException: Error: Call to a member function sendmail() on a non-object in" DO I get you right, that I can't give parameters from the controller to the container? Is there any possibility to configure the parameters for the mail in a separate class or ini-file? – Micha Nov 16 '14 at 12:18

1 Answers1

0

Your service has an external dependency, notably the mailer service. You can either inject the service container itself, or inject the mailer service. If your service only requires the mailer service and nothing else, I would suggest injecting just the mailer service. Here is how you would configure the DIC to inject the mailer service using a setter:

services:
    pw_mailer:
        class:     Pso\LogBundle\PwMailer
        arguments: [mailer]
ReynierPM
  • 17,594
  • 53
  • 193
  • 363