@franse You're confused because you're applying NodeJS advice to PHP
Edit: My bad, you're right, nice catch!
@Hyperbytes the PHP script you mentioned will only run those two instructions, if you run it from an API Action it'll just return an empty response body, it'll not influence the main PHP script. However, if you copy those two lines to the mailer's PHP script, maybe that could work
Edit 2:
Even if @franse is correct, @Hyperbytes code contains the set_time_limit instruction, so it's still useful
Thanks, guys, for the inputs. I've tested it, and I think the script is not being aborted if I close the page. The only problem now is that the page cannot be navigated away from when the script status is 'pending'. That's probably another issue that I need to fix, but I got the idea (that it was a bad idea, lol).
Oh! That issue is tricky to solve... It's related to sessions, only one script can be running at the same time for the same session... This is a PHP thing.
*The Server Action (SA) API isn't aborted by default if I close the page while it's pending, so there's no need to modify any code.
*I managed to work around the session locking or synchronous script issues (whatever they are) by using the Fetch API. Instead of using a dynamic event, I use a static event on success/done to call the SA mail API using a JavaScript function. This way, my mail script can run in the background without interfering with the current page.