title: Symfony2 - How to call Swiftmailer service in the controller
body: <p>I'm still learning on how to setup Swiftmailer as a service, I believe I have a working solution but need some help on how to call this in the controller.</p>
  
  <p><strong>How do I call this service in my controller?</strong> (service code, original code before service and service.yml below)</p>
  
  <p>Edit:
  I am trying to call it like so:</p>
  
  <pre><code>$emailManager = $this-&gt;container-&gt;get('email_manager');
  
  $content = $emailManager-&gt;sendMail($subject, $recipientName, $recipientEmail, $bodyHtml, $bodyText);
  </code></pre>
  
  <p>But am getting a undefined variable error:</p>
  
  <p><code>Notice: Undefined variable: subject in /.../DefaultController.php line 58</code></p>
  
  <p><strong>EmailManager service</strong></p>
  
  <pre><code>namespace Acme\EmailBundle\Service;
  
  use Symfony\Component\HttpFoundation\RequestStack;
  
  
  class EmailManager
  {
  private $request;
  private $mailer;
  
  public function __construct(RequestStack $requestStack, \Swift_Mailer $mailer)
  {
      $this-&gt;request = $requestStack-&gt;getCurrentRequest();
      $this-&gt;mailer  = $mailer;
  }
  
  public function sendMail($subject, $recipientName, $recipientEmail, $bodyHtml, $bodyText)
  {
      /* @var $mailer \Swift_Mailer */
      if(!$this-&gt;mailer-&gt;getTransport()-&gt;isStarted()){
          $this-&gt;mailer-&gt;getTransport()-&gt;start();
      }
  
      /* @var $message \Swift_Message */
      $message = $this-&gt;mailer-&gt;createMessage();
      $message-&gt;setSubject($subject);
  
      $message-&gt;setBody($bodyHtml, 'text/html');
      $message-&gt;addPart($bodyText, 'text/plain', 'UTF8');
  
      $message-&gt;addTo($recipientEmail, $recipientName);
      $message-&gt;setFrom( array('example@gmail.com' =&gt; 'Chance') );
  
      $this-&gt;mailer-&gt;send($message);
      $this-&gt;mailer-&gt;getTransport()-&gt;stop();
  }
  }
  </code></pre>
  
  <p><strong>Original controller code for sending emails prior to putting it in as a service</strong></p>
  
  <pre><code>/**
   * @Route("/", name="contact")
   * @Template("AcmeEmailBundle:Default:index.html.twig")
   */
  public function contactAction(Request $request)
  {
  $form = $this-&gt;createForm(new ContactType());
  
  if ($request-&gt;isMethod('POST')) {
      $form-&gt;submit($request);
  
      if ($form-&gt;isValid()) {
          $message = \Swift_Message::newInstance()
              -&gt;setSubject($form-&gt;get('subject')-&gt;getData())
              -&gt;setFrom($form-&gt;get('email')-&gt;getData())
              -&gt;setTo('example@gmail.com')
              -&gt;setBody(
                  $this-&gt;renderView(
                      'AcmeEmailBundle:Default:index.html.twig',
                      array(
                          'ip' =&gt; $request-&gt;getClientIp(),
                          'name' =&gt; $form-&gt;get('name')-&gt;getData(),
                          'message' =&gt; $form-&gt;get('message')-&gt;getData()
                      )
                  )
              );
  
          $this-&gt;get('mailer')-&gt;send($message);
  
          $request-&gt;getSession()-&gt;getFlashBag()-&gt;add('success', 'Your email has been sent! Thanks!');
  
          return $this-&gt;redirect($this-&gt;generateUrl('contact'));
      }
  }
  
  return array(
      'form' =&gt; $form-&gt;createView()
  );
  }
  </code></pre>
  
  <p><strong>services.yml</strong></p>
  
  <pre><code>services:
  email_manager:
      class: Acme\EmailBundle\Service\EmailManager
      arguments: [@request_stack, @mailer]
      scope: request
  </code></pre>