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

2021-12-29 00:00:00 php zend-framework zend-mail

我在 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.

相关文章