在php上单独的线程发送电子邮件 [英] Email on separate thread in php

查看:173
本文介绍了在php上单独的线程发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在php中启动一个单独的线程来发送和发送电子邮件。
我有一个小型Web服务,它从iPad应用程序获取一些信息,然后将数据插入数据库,然后发送电子邮件。问题在于,有时电子邮件需要太长的时间才能发送,iPad的请求超时。有没有办法我可以在单独的线程上发送电子邮件?我基本上想告诉iPad在电子邮件发送之前一切都是成功的。

I'm wondering if there is a way to start a separate thread in php to send and email. I have a small web service which takes some information from an iPad app and then inserts data into a db and sends an email afterwards. The problem is that sometimes the email takes too long to send and the iPad's request times out. Is there a way I could send the email on separate thread? I basically want to tell the iPad everything was successful before the email sends.

一些示例代码:

... Process info and insert into DB
echo "success"; //this should be returned to the iPad right away.

//start new thread here or possibly fork???
$email->send();

谢谢!

推荐答案

正如所指出的,PHP没有多线程能力,但它确实具有多处​​理功能。您可以创建并调用第一个调用处理电子邮件的第二个PHP。这个脚本需要能够在命令行上运行。

As pointed out, PHP doesn't have multithreading abilities, but it does have multi-processing functionality. You can create and call a second PHP that the first one would call to process the email. This script would need to be able to run on the command line.

exec('nohup php emailscript.php >/dev/null 2>&1 &');

拥有nohup和其后的所有内容非常重要。正在做的是将过程放在后台并重定向所有输出。否则PHP会等待它完成并返回一些东西。 nohup将确保脚本在父进程结束时不被操作系统杀死。

It is very important to have the nohup, and everything after it. What that is doing is putting the process in the background and redirecting all output. Otherwise PHP will wait for it to finish and return something. The nohup will make sure the script isn't killed by the OS when the parent calling process ends.

您需要以某种方式将电子邮件信息传递给脚本。您可以将信息放在数据库中,并传递记录ID,将信息作为参数传递,或传递其他多个选项。

You do need to somehow pass the email information to the script. You can put the information in a database and pass it a record ID, pass the information as parameters, or a number of other options.

这篇关于在php上单独的线程发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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