发送邮件而不阻止“执行" [英] Send mail without blocking 'execution'

查看:17
本文介绍了发送邮件而不阻止“执行"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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('recipient@domain.com');$mail->setSubject('网络联系人');$mail->setBodyHtml($mailContent, 'UTF-8');$this->_flashMessenger->addMessage('感谢您的留言!');$this->_redirector->setExit(false)->gotoUrl('/about/contact');$mail->send();

其中 $this->_redirector 是 *Zend_Controller_Action_Helper_Redirector* 的一个实例

这似乎没有什么区别,在发送邮件时脚本仍然被阻止,然后发生重定向.

也许我应该写一个控制器插件,使用 postDispatch() 钩子可以让我在访问者被重定向后发送邮件吗?

欢迎提出建议!

解决方案

为什么不试试这个:

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

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('recipient@domain.com');
$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!

解决方案

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.

这篇关于发送邮件而不阻止“执行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆