发送邮件而不阻止“执行”
我在 Zend Framework 应用程序中使用 Zend_Mail 来发送包含基于 Web 的联系表单内容的电子邮件。
邮件本身工作正常(我使用 Google Apps 帐户),但处理时间可能相当长(从几秒到近一分钟不等)。
我的控制器操作通常会在发送邮件后重定向访问者,因此我认为我可以在调用 $mail->send() 之前重定向访问者并让脚本在“后台”继续:
所以我尝试了以下:
$mailView = clone $this->view;
$mailView->assign('name', $form->getValue('name'));
$mailView->assign('email', $form->getValue('email'));
$mailView->assign('message', $form->getValue('message'));
$mailContent = $mailView->render('mailContact.phtml');
$mail = new Zend_Mail();
$mail->addTo('[email protected]');
$mail->setSubject('Web Contact');
$mail->setBodyHtml($mailContent, 'UTF-8');
$this->_flashMessenger->addMessage('Thank you for your message!');
$this->_redirector->setExit(false)->gotoUrl('/about/contact');
$mail->send();
其中 $this->_redirector
是 *Zend_Controller_Action_Helper_Redirector* 的实例
这似乎没有什么区别,在发送邮件并发生重定向之后,脚本仍然被阻止。
也许我应该编写一个控制器插件,使用 postDispatch() 挂钩允许我在访客重定向后发送邮件吗?
欢迎提出建议!
I'm using Zend_Mail in a Zend Framework application to send an e-mail with the contents of a web-based contact form.
The mailing itself works fine ( im using a Google Apps account ) but it can take fairly long to process ( ranging from a few seconds to nearly a minute ).
My controler action would normally redirect the visitor after sending the mail, so I thought I might be able to redirect the visitor prior to calling $mail->send() and let the script continue in the 'background':
So I tried the following:
$mailView = clone $this->view;
$mailView->assign('name', $form->getValue('name'));
$mailView->assign('email', $form->getValue('email'));
$mailView->assign('message', $form->getValue('message'));
$mailContent = $mailView->render('mailContact.phtml');
$mail = new Zend_Mail();
$mail->addTo('[email protected]');
$mail->setSubject('Web Contact');
$mail->setBodyHtml($mailContent, 'UTF-8');
$this->_flashMessenger->addMessage('Thank you for your message!');
$this->_redirector->setExit(false)->gotoUrl('/about/contact');
$mail->send();
where $this->_redirector
is an instance of *Zend_Controller_Action_Helper_Redirector*
This doesn't seem to make a difference, the script is still blocked while the mail is sent after which the redirection occurs.
Perhaps I should write a Controller Plugin, would using a postDispatch() hook allow me to send the mail after the visitor has been redirected?
Suggestions are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 PHP 不支持多线程编程,因此唯一想到的就是执行处理邮件发送的不同程序。
如果您可以控制主机,则可以以非阻塞方式使用 exec()。
检查此线程 - http://www.phpbuilder.com/board/showthread.php ?t=10351142 了解如何操作
Since PHP does not support multi-threaded programming, the only thing that comes to mind is to execute different program that handles the mail send.
If you have control over your host, you can use exec() in a non-blocking fashion.
Check this thread - http://www.phpbuilder.com/board/showthread.php?t=10351142 for how to do it
我想建议使用 cron jobs ,它相对简单、稳定,而且很适合您,
这里有一些关于 ZF + cronjobs 的链接:
祝你好运 !
I would like to suggest to use cron jobs , its relatively easy , stable , and simply fits you
here some links about ZF + cronjobs :
Goodluck !
为什么不试试这个:
从将加载的视图中
控制器负责
发送电子邮件。
Why don't you try this:
from within the view that will load
the controller responsible for
sending the email.
PHP默认不允许多线程。完成工作的一些方法:
使用一些消息队列服务,例如 IronMQ [推荐方法],这使得安全API/cURL 调用您的系统并生成一个不阻塞执行的单独进程。
在 PHP 中使用输出缓冲并在最终开始邮件发送操作之前使用 ob_flush。
使客户端在上次操作成功后再次通过 AJAX 调用服务器。
在您的服务器上安装 pthreads PECL 扩展
PHP doesn't allow multi-threading by default. Some ways to get your work done:
Use some message queue service like IronMQ [recommended approach] which makes a secure API/cURL call to your system and spawns off a separate process w/o blocking execution.
Use output buffering in PHP and use ob_flush before finally starting the mail sending operation.
Make the client call the server again via AJAX on success of previous operation.
Install pthreads PECL extension on your server