How to make a long PHP script display a successful message?

goal

I am trying to create a PHP script that emails a rather large (6MB) PDF file to users on the website requesting it. The user enters their email address into the form, submits the form, and the file is sent to them through the PHP Mailer instance. When submitting the form, the user displays a success page.

Problem

After the data is transferred using the POST method, there is a long pause, and the server will eventually return 404. However, in a few minutes the email will be received perfectly with the PDF attachment.

Troubleshooting / Attempts

I attribute the PHP Mailer problem by simply taking too long to send an email due to a large attachment. The server shuts down and returns to return 404. At the same time, the script ultimately completes the processing and after that receives an email.

If I delete the attachment and just send an empty letter, the script will load very quickly and display a success / confirmation page.

I looked at creating a redirect, but wherever I found explanations on how to achieve a redirect in PHP, it says that you should kill the original script (which I don't want to do).

Question

How to allow an email script to spend its time launching while displaying a success message to the user so they don’t get confused?

+4
source share
5 answers

This is a task for the message queue. Keep all the information necessary to send mail to the queue, and you have a background task that takes this information and sends your letters. If the insertion into the message queue was successful, display a success message for the user.

If you do not have access to background scripts (for example, to shared hosting), you can still get a direct answer. Just use ignore_user_abort(true) and send the correct Content-Length header. Browsers will trust this header and show the answer, and your script can continue to work and send mail.

+1
source

Another solution: do a quick trick, rather than immediately add a large file to your email. Give them a link to download the PDF file. Thus, the content becomes too short. But there is no attachment.

You can try to create a unique URL for downloading the same PDF file by different users.

Otherwise, the order of the email messages is good, as discussed.

+1
source

Instead of immediately sending the file by e-mail, save the request in the database with all the details necessary for sending it. Then you can regularly use the cronjob / schedule task (every minute, if you want), check the database for any insecure queries, and send emails in the background.

0
source

Since no codes were provided, you can use AJAX to invoke your script and display a success message wherever you want. The script will be called, the user will receive a message, and as soon as the script finishes, the user will receive an email.

Of course, a success message should not be implemented in AJAX handlers, otherwise the message will also be delayed.

0
source

This is a good precedent for an asynchronous job queue, such as Gearman or Beanstalk .

You need to start an external workflow that will wait for tasks from the job server.
On the web server side, just create a task, send it to the job server to send it to the workflow, and exit the PHP script.
The workflow then proceeds to send emails while the web server can continue to perform HTTP requests.

0
source

All Articles