A
A
Alexander2017-05-07 10:34:07
symfony
Alexander, 2017-05-07 10:34:07

How to make a service for sending mail using Swiftmailer?

I want to make sending mail to a separate service
created a class

namespace AppBundle\Utils;

class SendOrder
{
    protected $twig;
    protected $mailer;

    public function __construct(\Twig_Environment $twig, \Swift_Mailer $mailer)
    {
        $this->twig = $twig;
        $this->mailer = $mailer;
    }

    public function sendOrderAction($book)
    {

        $body = $this->twig->render('AppBundle:Email:send_order.html.twig', array('name' => $book));

        $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody($body);

        $this->mailer->send($message);
    }
}

#services,yml
    app.sendorder:
        class: AppBundle\Utils\SendOrder

I get the error
Catchable Fatal Error: Argument 1 passed to AppBundle\Utils\SendOrder::__construct() must be an instance of Twig_Environment, none given, called
in

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2017-05-07
@Austin_Powers

You have two input parameters in the constructor. When registering a service, you do not transfer any.
Read about arguments in services. symfony.com/doc/current/service_container.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question