我正在从我的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" -> "factories" 下
'SendEmail' => 'Application\Service\SendEmail',
3> 在IndexController中定义了该函数
public function getSendEmail( ) {
if ( !$this->sendEmail ) {
$this->sendEmail = $this->getServiceLocator( )->get( 'SendEmail' );
}
return $this->sendEmail;
}
我想在发送电子邮件的地方使用以下代码:
$sendEmail = $this->getSendEmail( );
$sendEmail->sendEMail("subject of email","email body message", "from@email.com", "to@email.com");
我添加了Application\Service\SendEmail到可调用列表中。
我遇到了以下错误。
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、Eight-Bit Guru、Tad Donaghe小组取消保留,以便我可以发布解决方案。