ZF2定义发送电子邮件的服务

时间:2014-05-09 18:02:41

标签: email service zend-framework2

我正在从我的ZF2应用程序发送电子邮件。我想将其创建为服务,以便我可以从任何模块和任何控制器使用它。

我有发送电子邮件的代码,需要指导来创建服务。

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;
}

我目前正在其中一个控制器中定义此功能来发送电子邮件。但是,我的应用程序正在发展,我需要从模块中的多个模块和多个控制器发送应用程序。 我理解,如果我将电子邮件定义为服务并根据需要调用它,这将更容易。我已阅读文档,而且相当沉重。我正在寻找有人做过类似事情的例子。 提前感谢您的时间。

2014-05-11第二次编辑 我做了以下

1>使用以下代码

创建了一个新的服务类SendEmail
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 - ;在module.config.php中为应用程序模块定义了一个条目 在“service_manager”下 - > “工厂”

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

3>在IndexController中定义了函数

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

4>在我想发送电子邮件的地方我有以下代码

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

5个我将Application \ Service \ SendEmail添加到了invokables列表中

我收到以下错误

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}

基本上没有创建服务;我错过了什么;还有什么需要做的。

编辑于2014-05-12找到解决方案;不确定这是不是最好的方法!!!

我会要求AlexP,nikoshr,EdChum,八位大师,Tad Donaghe小组取消暂停,以便我可以发布解决方案。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,我发布了适用于我的代码;对可以做出的任何其他改进做评论。

1>使用以下代码创建了一个新的服务类SendEmail 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 - ;在module.php中为应用程序模块定义,创建函数getServiceConfig()

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

3>在IndexController

中定义了该函数
public function getSendEmail( ) {
    if ( !$this->sendEmail ) {
        $this->sendEmail = $this->getServiceLocator( )->get( 'SendEmail' );
    }
    return $this->sendEmail;
}

4>在我想发送电子邮件的地方我有以下代码

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