Email on separate thread in php

后端 未结 2 1656
南方客
南方客 2021-02-19 10:43

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 d

2条回答
  •  时光说笑
    2021-02-19 11:24

    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 &');
    

    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.

    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.

提交回复
热议问题