1

I'm following Symfony 3 Documentation to send an email from my controller, but I'm getting an error. I made sure to follow each step from the above page correctly, but no use.

This is how my controller looks like:

<?php

namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     */
    public function indexAction(\Swift_Mailer $mailer)
    {
        $message = (new \Swift_Message('Hello Email'))
            ->setFrom('send@example.com')
            ->setTo('my.address@gmail.com')
            ->setBody(
                $this->renderView(
                    // app/Resources/views/Emails/registration.html.twig
                    'MyBundle::Emails/registration.html.twig',
                    array('name' => 'John Doe')
                ),
                'text/html'
            );

        $mailer->send($message);

        return new Response('<html<body>Sent</body></html>');
    }
}

This is the error I'm getting:

Controller "MyBundle\Controller\DefaultController::indexAction()" requires that you provide a value for the "$mailer" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

Comparing to the documentation page, I'm pretty sure everything is as it should be, so can anyone please help me find the source of this error?

Томица Кораћ
  • 2,542
  • 7
  • 35
  • 57

2 Answers2

3

Simply remove the $mailer from your constructor (you are not in a service, you are in a controller) , and use $this->get('mailer')->send($message); to send. (again, you are in a controller, you have access to the mailer service, no need to try and inject it into the constructor)

Sam Janssens
  • 1,491
  • 1
  • 12
  • 30
  • 1
    Thank you so much, this worker. Man, I wish Symfony had better documentation. Very misleading and confusing. I wouldn't be able to figure this out from the docs in a million years. – Томица Кораћ Jul 05 '17 at 10:45
0

I think, you not enable autowiring in projects settings. So, Swift_Mailer is not injected automatically. Enable it or inject mailer service manually.

See more about autowiring in Symfony here.

maximkou
  • 5,252
  • 1
  • 20
  • 41
  • Thanks for the answer, but isn't the trailing backslash meant to do just that? Further more, on your link it says that by default autowiring is enabled, so where (and how) can I check if you are correct? Lastly, I can't seem to find analogy between the autowiring example and my controller. Could you please provide an example code? – Томица Кораћ Jul 05 '17 at 10:42
  • 1
    no need for autowiring here, you are in a controller (extending Symfony\Bundle\FrameworkBundle\Controller\Controller), so you already have access to the container, and all services – Sam Janssens Jul 05 '17 at 10:43
  • 1
    @SamJanssens, Firstly, I think, explicit definition is better than call service locator. Second, In my answer I write about manual resolving service. Third, i think, topicstarter have Symfony < 3.3, but see docs for 3.3 – maximkou Jul 05 '17 at 10:48