Get the mail()
to do it in php while calling the script correctly for it to persist until the end of the operation even the user agent is closed.
This answer describes how to do it:
It's a race condition. PHP will detect at some point (usually upon
attempting to do output) that Apache is yelling in its face that the
remote user has closed the connection. Whether everything you wanted
to do is done at that point depends on how your code's structured.
If you want to ensure that all operations are complete before the
script shuts itself down, use ignore_user_abort(TRUE), which keeps PHP
running after the connection is severed. It's still subject to the
user max_execution_time limits and whatnot, but it will not shut down
because you disconnected.
And this answer shows you an accurate way to do it:
Certainly it can be done with PHP, however you should NOT do this as a
background task - the new process has to be dissocated from the
process group where it is initiated.
Since people keep giving the same wrong answer to this FAQ, I've
written a fuller answer here:
http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html