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

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

问题描述

我在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('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()钩子使我在重定向访问者之后发送邮件?

欢迎提出建议!

解决方案

您为什么不尝试以下操作:

  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天全站免登陆