5

I am using Symfony2 and FOSUserBundle

I have to send email using SwiftMailer in my mailer class which is not a controller or its action. I am showing what I have coded

<?php

namespace Blogger\Util;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class FlockMailer {


    public function SendEmail(){
        $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('to@example.com')
        ->setBody('testing email');

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

But I am getting the following error

Fatal error: Call to undefined method Blogger\Util\FlockMailer::get() ....

How can I proceed?

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Umair
  • 393
  • 1
  • 7
  • 20
  • I don't get it. Is that code snippet meant as your current solution but you'd like to move mailing to someplace else? If that's correct, you should probably read about injecting services into your custom classes: http://stackoverflow.com/questions/6124444/how-can-i-access-a-service-outside-of-a-controller-with-symfony2 – geca Apr 20 '12 at 13:09
  • I have used FOSUserBundle and FOSFacebookbundle what i want when user successfully login with facebook account i want to send email to user with his password so that he can login with that email password for this i have to write function in provider class to send email .... – Muhammad Umair Apr 20 '12 at 13:17

2 Answers2

8

EDIT: as i din't tested the code you should also specify the transport layer if you don't use the service container for getting the instance of the mailer. Look at: http://swiftmailer.org/docs/sending.html

You're doing it wrong. You basically want a service, not a class that extends Controller. It's not working because service container is not available in SendMail() function.

You have to inject the service container into your own custom helper for sending email. A few examples:

namespace Blogger\Util;

class MailHelper
{
    protected $mailer;

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

    public function sendEmail($from, $to, $body, $subject = '')
    {
        $message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($from)
            ->setTo($to)
            ->setBody($body);

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

To use it in a controller action:

services:
    mail_helper:
        class:     namespace Blogger\Util\MailHelper
        arguments: ['@mailer']

public function sendAction(/* params here */)
{
    $this->get('mail_helper')->sendEmail($from, $to, $body);
}

Or elsewhere without accessing the service container:

class WhateverClass
{

    public function whateverFunction()
    {
        $helper = new MailerHelper(new \Swift_Mailer);
        $helper->sendEmail($from, $to, $body);
    }

}

Or in a custom service accessing the container:

namespace Acme\HelloBundle\Service;

class MyService
{
    protected $container;

    public function setContainer($container) { $this->container = $container; }

    public function aFunction()
    {
        $helper = $this->container->get('mail_helper');
        // Send email
    }
}

services:
    my_service:
        class: namespace Acme\HelloBundle\Service\MyService
        calls:
            - [setContainer,   ['@service_container']]
gremo
  • 47,186
  • 75
  • 257
  • 421
  • I have implemented without accessing the service container following error come====== Catchable Fatal Error: Argument 1 passed to Swift_Mailer::__construct() must be an instance of Swift_Transport, none given, – Muhammad Umair Apr 20 '12 at 13:31
  • @MuhammadUmair yes, you have to specify the transport layer. I didn't tested the code. You should follow http://symfony.com/doc/current/cookbook/email/email.html – gremo Apr 20 '12 at 13:34
1

Just forget about the setter and the getter:

$transport = \Swift_MailTransport::newInstance();
$mailer = \Swift_Mailer::newInstance($transport);
$helper = new MailHelper($mailer);
$helper->sendEmail($from, $to, $body,$subject);

That worked for me with the MailHelper called from a listener method.

Wiert
  • 26
  • 1