Reputation: 174
I am having a PHP web page which has a button. Whenever the user click this button, an AJAX request is sent to the server, and an email is sent back to him. A notification is also displayed on the screen. (It's similar to commenting on Facebook, the message is displayed, while the server keeps working to send emails).
Since sending email takes time, I want the server to return the output to the user first, then keeps working to send email.
Anyone knows how to achieve this in PHP? Thanks.
Upvotes: 0
Views: 195
Reputation: 48367
Really this is the same question as I am running through a php background process problem
And as per my answer there, if the email is taking a long time to process then there's something wrong with your MTA config.
But assuming that might be something you can't fix, you might consider sending the email using a callback invoked via register_shutdown_function() after your code does an explicit exit;
Upvotes: 1
Reputation: 141877
Sending an email from php shouldn't take very long, since all it has to do is send a message to the mailserver, which can take it's time sending the email after.
However, to answer your question, you can execute another php script as a background process, and have that script send the email. On a linux box it's as easy as:
exec('php phpscript.php > /dev/null &');
Which will execute phpscript.php
as a background process, so that your current script won't wait for it to finish. If you want you can redirect the output somewhere other than /dev/null so you can actually view the output later.
You might need to use php-cli
instead of php
. Depending on how your server is set up.
Upvotes: 0