0

我正在编写一个 php 脚本来向特定邮件列表中的所有电子邮件地址发送邮件。邮件可能会达到数千封,整个脚本甚至可能需要几个小时才能执行。我想要的是,一旦用户输入邮件详细信息、正文、主题等并点击按钮/链接,php 脚本就会在后台运行,用户一旦点击按钮/链接就会被重定向到某个页面。

简而言之,php 脚本应该从浏览器启动,但即使浏览器关闭也应该在服务器上运行。

或者,如果可以使用 AJAX/Javascript 在重定向页面上显示进度条,但即使浏览器关闭,php 脚本也应继续执行。

4

3 回答 3

2

PHP is the wrong tool for the job. Set up a mailing list on a e-mail server (perhaps the same server), and from the browser hand the data to PHP via ajax, which should then send the e-mail (only once) to the mailing list.

The e-mail software will take care of sending a copy to every subscriber of the mailing list.

This is one of the most efficient ways of doing it.

Some links which may help you do more research:

Remember though, this has nothing to do with programming, and you should ask for help on some other stack exchange site (like unix or serverfault).

于 2012-10-01T22:50:09.610 回答
0

To get both the script to continue and having it giving it status even though the user refresh this is a bit of work.

The super easy approach is to drop the the second part. If you just need a action to be executed server side you could have a XHR (aka ajax) and give the progression with a long polling approach.

If you want the process to be independent from the user connection you need to spawn a new PHP instance each time. and then find a way to get its output to the user. You could store the output into a redis for example.

于 2012-10-01T22:54:00.157 回答
0

您想要做的是以下内容:

单击按钮调用您的处理脚本。然后,处理脚本应使用ignore_user_abort来处理请求,以防止脚本在浏览器继续移动时停止,然后使用header('Location: success.html'); 重定向broser;

<?php
  ignore_user_abort(true);
  header('Location: success.html');
  echo 'Making sure the header gets flushed';
  flush();

  //Send out the mails
?>

但是,您需要确保脚本没有活动的输出缓冲。如果您正在使用一些压缩模块或任何可能等待整个过程完成后再交付内容并因此冻结整个内容的东西。最好是禁用脚本 url 的任何输出缓冲。

这类似于您在 PHP 中创建守护程序的方式。

于 2012-10-03T09:33:30.577 回答