PHP Wait for a PDF file to be created and then mailed it?

I wrote this script that sends a receipt to the client about his order, but he has a problem:

He will not wait for the PDF script.

Thus, it just requires a PDF script and starts to execute it and sends mail while the PDF script is still working on the PDF. I am sure there is a way to postpone the email script, but to complicate things:

All order.php is executed with a jquery ajax call, and the script will wait for php to complete, and then tells the browser that the request has been completed. Thus, he can wait more than five minutes for the client to wonder why he took so long.

Therefore, I need him to wait for the creation of the PDF file, and then send the mail, but he should not leave the client waiting.

Here is my code:

<?php $addid = "orderid.txt"; $current = file_get_contents($addid) + 1; echo $current; file_put_contents($addid, $current); ?> <?php // Lue tilauksen ID sähköpostia varten $orderid = "orderid.txt"; $ordernumber = file_get_contents($orderid); // Kirjoita kuitti require('receipt.php'); ?> <?php //Lähetä tilausvahvistus require_once('mail/class.phpmailer.php'); $path = "kuitit/kuitti".$orderid.".pdf"; $bodytext = ' Olemme vastaanottaneet tilauksenne '. $ordernumber .'. Tilaamanne tuotteet löytyvät liitteestä.' ; $email = new PHPMailer(); $email->From = ' no-reply@coverlinefilms.fi '; $email->FromName = ' no-reply@coverlinefilms.fi '; $email->Subject = 'Olemme vastaanottaneet tilauksenne ' . $ordernumber; $email->Body = $bodytext; $email->AddAddress(' christian.nikkanen@gmail.com '); $email->AddAttachment($path, 'kuitti777.pdf'); return $email->Send(); ?> 

Pdf script

+4
source share
1 answer

In your case (and in most active CPU operations) I usually use a queue with two cronjobs : the first will generate pdf files and mark them as ready-made in the database table. The second cronjob will query the database to check if the file is in the ready format, and then send an email with the attachment.

Thus, in the best scenario, you will receive an empty queue: all receipts are sent as soon as they are ready, in the worst case, the system will increase the sending of all emails until the PDF queue is empty.

Hope this approach is clear. You can find some links to PHP daemons (IMHO is a better way to manage tasks than a regular PHP script) here .

0
source

Source: https://habr.com/ru/post/1432977/


All Articles