Question

I am sending emails from my ZF2 application. I want to create it as a service so that I can use it from any module and any controller.

I have the code to send the emails, need guidance to create service.

protected function sendEMail($msgSubj,$msgText, $fromEmail, $toEmail) {
    $mail = new Mail\Message();
    $mail->setFrom($fromEmail, $fromEmail);
    $mail->addTo($toEmail, $toEmail);
    $mail->setSubject($msgSubj);
    $mail->setBody($msgText);
    $transport = new Mail\Transport\Sendmail();
    $transport->send($mail);
    return true;
}

I am currently having this function defined in one of the controllers to send the email. However, my application is evolving and I need to send application from multiple modules and multiple controllers within modules. I understand that this will be easier if I define sending email as a service and call it as required. I have read the documentation and it is considerably heavy. I am looking for examples where someone has done something similar. Thanks in advance for your time.

2nd Edit on 2014-05-11 I have done the following

1> created a new service class SendEmail with the following code

namespace Application\Service;


class SendEmail {

    public function __construct()
    {
    }

    protected function sendEMail($msgSubj,$msgText, $fromEmail, $toEmail) {
        $mail = new \Zend\Mail\Message();
        $mail->setFrom($fromEmail, $fromEmail);
        $mail->addTo($toEmail, $toEmail);
        $mail->setSubject($msgSubj);
        $mail->setBody($msgText);
        $transport = new \Zend\Mail\Transport\Sendmail();
        $transport->send($mail);
        return true;
    }

}

2> defined an entry in module.config.php for the Application Module under "service_manager" -> "factories"

'SendEmail' => 'Application\Service\SendEmail', 

3> defined the function in IndexController

public function getSendEmail( ) {
    if ( !$this->sendEmail ) {
        $this->sendEmail = $this->getServiceLocator( )->get( 'SendEmail' );
    }
    return $this->sendEmail;
}

4> at the place where I want to send the email I have the following code

$sendEmail = $this->getSendEmail( );
$sendEmail->sendEMail("subject of email","email body message", "from@email.com", "to@email.com");

5> I added Application\Service\SendEmail to the list of invokables

I am getting the following error

Zend\ServiceManager\Exception\ServiceNotCreatedException
File:
/var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:1036
Message:
While attempting to create sendemail(alias: SendEmail) an invalid factory was registered for this instance type.
Stack trace:
#0 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(612): Zend\ServiceManager\ServiceManager->createFromFactory('sendemail', 'SendEmail')
#1 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(572): Zend\ServiceManager\ServiceManager->doCreate('SendEmail', 'sendemail')
#2 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(504): Zend\ServiceManager\ServiceManager->create(Array)
#3 /var/www/zf2/dwam/module/UserAdmin/src/UserAdmin/Controller/IndexController.php(123): Zend\ServiceManager\ServiceManager->get('SendEmail')
#4 /var/www/zf2/dwam/module/UserAdmin/src/UserAdmin/Controller/IndexController.php(77): UserAdmin\Controller\IndexController->getSendEmail()
#5 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): UserAdmin\Controller\IndexController->userLoginAction()
#6 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#7 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#8 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#11 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#12 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#13 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 /var/www/zf2/dwam/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(316): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#15 /var/www/zf2/dwam/public/index.php(17): Zend\Mvc\Application->run()
#16 {main}

Basically the service has not been created; what am I missing; What more needs to be done.

Edit on 2014-05-12 Found the solution; not sure if this is the best way though!!!

I would request the group of AlexP, nikoshr, EdChum, Eight-Bit Guru, Tad Donaghe to remove the hold so that I can publish the solution.

Was it helpful?

Solution

I have found the solution and I am posting the code that work well for me; Do comment on any other improvements that can be made.

1> created a new service class SendEmail with the following code namespace Application\Service;

class SendEmail {

    public function sendEMail($msgSubj,$msgText, $fromEmail, $toEmail) {
        $mail = new \Zend\Mail\Message();
        $mail->setFrom($fromEmail, $fromEmail);
        $mail->addTo($toEmail, $toEmail);
        $mail->setSubject($msgSubj);
        $mail->setBody($msgText);
        $transport = new \Zend\Mail\Transport\Sendmail();
        $transport->send($mail);
        return true;
    }

}

2> defined in module.php for the Application Module create the function getServiceConfig()

'factories' => array (
    'SendEmail' => function ( $sm ) {
        return new \Application\Service\SendEmail( );
    },
), 

3> defined the function in IndexController

public function getSendEmail( ) {
    if ( !$this->sendEmail ) {
        $this->sendEmail = $this->getServiceLocator( )->get( 'SendEmail' );
    }
    return $this->sendEmail;
}

4> at the place where I want to send the email I have the following code

$sendEmail = $this->getSendEmail( );
$sendEmail->sendEMail("subject of email","email body message", "from@email.com", "to@email.com");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top