0

I have an "email sending" in my application (web). The user sends his name, email, subject and some text, and I send an email back to him (with phpmailer). This takes 10 secons (the entire process), but the validation (and response) of the information submitted just takes 2 seconds (using ajax).

¿How I can response in 2 seconds and leave the server sends the email (8 seconds) even if the user refresh the page?

I speak english very bad, sorry.

  • You might be looking for PCNTL: http://stackoverflow.com/questions/10871293/how-does-pcntl-fork-work-in-php – Kevin Kopf Feb 06 '16 at 23:46
  • The easiest solution is to simply detach from the client request by means of phps `ignore_user_abort()`. Then you can take all the time you need to finish your request, independent of what the user does. – arkascha Feb 06 '16 at 23:48
  • @arkascha need to response in 2 seconds. Thanks for comment. – Fabio Alberto Mayorga Duarte Feb 06 '16 at 23:56
  • @Nordenheim i thinks that works. Thanks. – Fabio Alberto Mayorga Duarte Feb 06 '16 at 23:56
  • Which is exactly what my suggestion will enable you to do. You get the values, validate them, send a confirmation, detach from the request and that's it from the users point of view. Your script continues in background and spends 8 more seconds (or whatever) to process the values and send out that message. – arkascha Feb 07 '16 at 08:56

1 Answers1

0

The best solution would be using async process.

User's request would just publish the data to some kind of message queue. (Amazon SQS, RabbitMQ).

On the other side of the queue you would have daemon running all the time. This daemon would consume messages from the queue and send emails. It can also be run in multiple instances to increase performance. Nice way to keep that daemon alive would be Supervisor (http://supervisord.org/)

/--- sender Producer ----->queue---- sender \--- sender \-- sender

Martin Strouhal
  • 1,174
  • 1
  • 12
  • 23