Demand:
In a system, upload a product patch file or release product update information, need to use the product users (more users) send an e-mail, sent after the prompt sent! Our common PHP code is written as follows:
// ......
Find all user email addresses that use the product, assuming it's stored in the array
for ($i =0; $i
<>
SendEmail ();
}
function SendEmail () {
Send mail code
}
//......
Let's say 100 messages are sent this time. What is the result of this operation? User experience: User wait-and-send message complete and return information (this period is very likely, script execution time out) This operation due to the need to send a large number of messages, resulting in PHP execution time is too long, the user irritable waiting.
When Apache or Nginx waits beyond the allowed execution time, a timeout error is returned.
This time the user is not clear whether this operation is successful or not, in the end sent a few emails. From this we can see that the above code user experience is very poor, and can not successfully complete the task.
So how to solve the problem of poor user experience?
A concept is mentioned here that executes asynchronously
User experience: User wait-to send complete friends will ask, how to lack the sending link?
OK, send a link in the user submits the request, the letter to the task to a separate processing of the PHP program processing, when the user saw "send complete" when the fact that the message has not been sent, this time, the sending process is working in the background, a lunar sealed outward sent.
$domain = "www.scutephp.com";
$url = "/sendemail.php";
$par = "Email=1@163.com,2@163.com,3@163.com&time=". Time ();
$header = "POST $url http/1.0\r\n";
$header. = "content-type:application/x-www-form-urlencoded\r\n";
$header. = "Content-length:". strlen ($par). " \r\n\r\n ";
$fp = @fsockopen ($domain, $errno, $errstr, 30);
Fputs ($FP, $header. $par);
Fclose ($FP);
Echo ' send complete ';