发送邮件而不阻止“执行”

发布于 2024-10-14 02:59:29 字数 1175 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

治碍 2024-10-21 02:59:29

由于 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

瑶笙 2024-10-21 02:59:29

我想建议使用 cron jobs ,它相对简单、稳定,而且很适合您,

这里有一些关于 ZF + cronjobs 的链接:

  1. 如何在 Zend Framework 中存储 cron 作业的脚本?
  2. http://www.god-object.com/2010/03/26/ bootstrap-zend-framework-for-use-in-cronjobs/
  3. http://jazzslider.org/2010/01/12/cron-tasks-in-zend-framework-apps
  4. 使用 Zend Framework 创建 cronjob >>>

经过一些研究和大量的拖延,我得出了一个简单的结论:ZF 化的 cron 脚本应该包含 zend 框架应用程序的所有功能 - 没有所有视图内容。我通过在应用程序目录中创建一个新的 cronjobfoo.php 文件来完成此操作。然后我从以下内容中获取了最低限度的内容:-我的前端控制器(index.php)-我的bootstrap.php

我把所有的视图东西都拿出来了
专注于保护环境
设置、数据库设置、自动加载器和
注册表设置。我不得不采取一点
是时候更正文档根目录了
变量并删除一些 OO
从我的复制功能
引导程序。

之后我就在我的
正在编译并通过电子邮件发送的情况
夜间报告。用起来很棒
Zend_Mail。当我确信
我的脚本按照我的方式运行
想要,我只是将其添加到我的 crontab 中。

祝你好运 !

I would like to suggest to use cron jobs , its relatively easy , stable , and simply fits you

here some links about ZF + cronjobs :

  1. How do you store your scripts for cron jobs in Zend Framework?
  2. http://www.god-object.com/2010/03/26/bootstrap-zend-framework-for-use-in-cronjobs/
  3. http://jazzslider.org/2010/01/12/cron-tasks-in-zend-framework-apps
  4. Create cronjob with Zend Framework >>

After some research and a lot procrastination I came to the simple conclusion that a ZF-ized cron script should contain all the functionality of you zend framework app - without all the view stuff. I accomplished this by creating a new cronjobfoo.php file in my application directory. Then I took the bare minimum from: -my front controller (index.php) -my bootstrap.php

I took out all the view stuff and
focused on keeping the environment
setup, db setup, autoloader, &
registry setup. I had to take a little
time to correct the document root
variable and remove some of the OO
functionality copied from my
bootstrap.

After that I just coded away.. in my
case it was compiling and emailing out
nightly reports. It was great to use
Zend_Mail. When I was confident that
my script was working the way I
wanted, I just added it my crontab.

Goodluck !

无声情话 2024-10-21 02:59:29

为什么不试试这个:

  1. 加载视图
  2. 调用 Ajax 脚本
    从将加载的视图中
    控制器负责
    发送电子邮件。

Why don't you try this:

  1. Load the view
  2. Call an Ajax Script
    from within the view that will load
    the controller responsible for
    sending the email.
空心空情空意 2024-10-21 02:59:29

PHP默认不允许多线程。完成工作的一些方法:

  1. 使用一些消息队列服务,例如 IronMQ [推荐方法],这使得安全API/cURL 调用您的系统并生成一个不阻塞执行的单独进程。

  2. 在 PHP 中使用输出缓冲并在最终开始邮件发送操作之前使用 ob_flush。

  3. 使客户端在上次操作成功后再次通过 AJAX 调用服务器。

  4. 在您的服务器上安装 pthreads PECL 扩展

PHP doesn't allow multi-threading by default. Some ways to get your work done:

  1. 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.

  2. Use output buffering in PHP and use ob_flush before finally starting the mail sending operation.

  3. Make the client call the server again via AJAX on success of previous operation.

  4. Install pthreads PECL extension on your server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文