I've written this script that sends customer a receipt of their order, but it has a problem:
It won't wait for the PDF script.
So it simply requires the PDF script, and starts executing it and sends the mail while the PDF script is still working on the PDF. I'm sure that there is a way to delay the email script, but to make thing more complicate:
The whole order.php is executed with jquery ajax call, and the script will wait for the php to finish and then reports browser that request was succeeded. So it could wait over five minutes while the client would be wondering why it's taking so long.
So I need it to wait for the PDF to be created and then send the mail, but it shouldn't leave the client waiting.
Here's 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();
?>