0

ok, So I am still a little new to Symfony 2 but I have been all over the web trying to see what I am doing wrong, but going over and over the Symfony docs, I can't see why its not working.

So I have tow points in my app that will need to send two new user emails, one with a password and one with an active path to verify the email. I am using MailGun to send the emails, this works fine. But has I have two controllers that will send these emails, I thought that if I wanted to change/edit them, it would be best if they where in the same place. So I build my own class for them to go in.

This is the problem, as it is not an controller I am trying to 'render' an Standard Email template layout. And for the life of me can not figure out why its not working.

So my Class:

namespace xxxBundle\Classes;

//use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class NewUserEmails {

   //private $templating;

   //public function __construct(EngineInterface $templating) {
      // $this->templating = $templating;
   //}

   private $container;

   public function __construct(ContainerInterface $container) {
      $this->templating = $container->get('templating');
   }

   public function SendActivePathEmail($GetNewUserID, $Token) {

    /* Send Active Path To New User - Before Password */
    $Email_Header_ActiveUser = 'Access';
    $Email_Content_ActiveUser = 'Please active your account, by clicking the link below.';
    $Email_Content_ActiveUser .= '<br/><br/><strong>PLEASE NOTE:</strong> Your account is not active untill you have vifyied your email';
    $Email_Content_ActiveUser .= '<br/><br/><p><a href="xxxxxx/active/'.$GetNewUserID.'/'.$Token.'">Active Account Link</a></p>';

    $ActiveUserEmail = $this->templating->render('xxxBundle:Emails:StandardEmail.html.twig', 
                                                                ['EmailContent' => $Email_Header_ActiveUser, 
                                                                 'EmailMessage' => $Email_Content_ActiveUser], 
                                                                 'text/html');
    return $ActiveUserEmail;
  }

  public function SendPswEmail($PlainPwd) {

    /* Send Password To New User */
    $Email_Header_NewPsw = 'Access';
    $Email_Content_NewPsw = 'This is your password <strong>' .$PlainPwd. '</strong> for xxxxx login';

    $PasswordEmail = $this->templating->render('xxxBundle:Emails:StandardEmail.html.twig', 
                                                                ['EmailContent' => $Email_Header_NewPsw, 
                                                                 'EmailMessage' => $Email_Content_NewPsw], 
                                                                 'text/html');
    return $PasswordEmail;
  }

} //Class End

Now this is what I have in my services YML file,

new_user_emails:
    class: xxxBundle\Classes\NewUserEmails
    arguments: [@service_container]

This is the services file within my bundle, which I know is being loaded has I have a login handler which works without any problems.

And this is how I am calling the class within my Controller,

$GetNewUserEmails = new NewUserEmails();

$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);

So has far as I can tell I am doing it right, but when I try to save the user (which does save without any problems) I get the following error,

Catchable Fatal Error: Argument 1 passed to xxxxBundle\Classes\NewUserEmails::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given

P.S. I have tried to insert just the tempting, but that gave me the same error!

All help most welcome,

What am I doing wrong?

Thanks,

1 Answer 1

2

You need to retrieve your service from the symfony container.

try this:

$GetNewUserEmails = $this->get('new_user_emails');

$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);

Instead of this:

$GetNewUserEmails = new NewUserEmails();

$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);

PS: Is not a good practice to pass the whole container to the service, but only the service it really needed.

Hope this help

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I have read that passing the whole container is bad practice, thats why I started with just passing the '@templating' - I give this a go, thanks for the fast answer!
That seems to work - but its a bit slow - I think thats my VM sending the emails via MailGun - not sure I have a good config somewhere on the VM. But should I be just passing the @templating then, rather than the whole container? If its such a bad idea to pass the whole thing, why does the symfony dev tools not warm you? If there is has much, as I think there is inside the container, then why even let us get away with using it?
pass the container is a bad practice but it works. The problem is about the design of the service that don't explicit define the needed dependency for work (as example, need the templating service instread of a db connection).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.