Send mail without blocking 'execution'

2019-05-29 05:15发布

问题:

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!

回答1:

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.


回答2:

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



回答3:

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 !



回答4:

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