PHP Asynchronous e-mail issue

Source: Internet
Author: User
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 ';

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.