dongzhang7157 2014-05-14 15:19
浏览 43
已采纳

PHP群发邮件与AJAX异步

Ok so basically I need to send 38,400 email from a regular hosting using HostGator. I'm currently using PHPMailer which works fine. What I do, is that the client simply fills an E-mail Template and I save all the E-mail content just so it's easier for me to handle the E-mail content.

When the client wants to send the E-mail I asynchronously call a php code to send the e-mails.

            $.ajaxSetup({cache: false});
            $.ajax({
                async:true,
                type: "POST",
                dataType: "html",
                contentType: "application/x-www-form-urlencoded",
                url:"operaciones.php",
                data:"idcorreo1="+idCorreo+"&operaciones=enviarPlantilla4",
                success:function(data){
                    console.log(data);
            },
                cache:false
            });

That is my AJAX code which basically calls the php process. Now my php code which is the called code:

                 if(isset($_REQUEST["idcorreo1"])){
                        $boletin = new Boletin();
                        $boletinesActivos = array();
                        $numBoletinesActivos = $boletin  -> contarBoletinesActivos();


                        $progresoMailing = new ProgresoMailing();
                        $idCorreo = $_REQUEST["idcorreo1"];
                        $tipoCorreo = 1;//correo1, tmpcorreo1;
                        $fechaYHoraInicio = date("Y-m-d h:i:sa");

                        $progresoMailing -> idCorreo = $idCorreo;
                        $progresoMailing -> tipoCorreo = $tipoCorreo;
                        $progresoMailing -> fechaYHoraInicio = $fechaYHoraInicio;
                        $progresoMailing -> numCorreos = $numBoletinesActivos;

                        $progresoMailing -> insertarProgresoMailing();



                        $i = 0;
                        while($i < $numBoletinesActivos){
                            $boletinesActivos = $boletin -> obtenerBoletinesConLimite($i);
                            foreach ($boletinesActivos as $boletinActivo) {
                                    $tempcorreo1= new tempcorreo4($idCorreo,0,$boletinActivo -> idBoletin);
                                    $tempcorreo1->enviar();
                                    $progresoMailing -> agregarUnEnviado();
                                    usleep(200000);
                            }
                            usleep(200000);
                            $i += 100;
                        }

                        $fechaYHoraFinal = date("Y-m-d h:i:sa");
                        $progresoMailing -> fechaYHoraFinal = $fechaYHoraFinal;
                        $progresoMailing -> status = 1;
                        $progresoMailing -> actualizarFechaYHoraFinal();
                        $progresoMailing -> actualizarStatus();

                    }

So this code what it does is tha it grabs all the registered e-mails in the database(BOLETIN) and since I don't want to load 38,400 objects in memory I limit the query to give me 100 results per query. After an e-mail has been sent (enviar) I make the process sleep .2 seconds just so the hosting doesn't go crazy. I handle a counter which tellms me number of e-mails sent, just so I can notifiy my client that the e-mails have been sent, and I display a progress bar, this is why I need to save the number of e-mails sent. So after an hour or so, the php process stopped around 11,000 e-mails were sent. I didn't find any failures or faults in the erro_log file, Why did the process stop? was it the OS? I appreciate any comments, tips or solutions to this problem. Thanks!!

  • 写回答

3条回答 默认 最新

  • drexlz0623 2014-05-14 15:40
    关注

    Most hosts (I think all of them that I have used) have limits on the number of emails an account is allowed to send either per hour or per day and probably both.

    If you look at the hostgator email policy http://www.hostgator.com/mailpolicy it clearly states there are limits - I'm surprised you have actually managed to get to 11,000 sent! That is assuming they have actually been sent and that is not just a counter of the send function working (there is a difference in asking for something to be sent and it actually going). They could all be coming back as undelivered.

    They recommend PHPlist which uses throttling to ensure you stay under you allowed limits and I have used it to send a large number (100k+) of emails in the past but unless you pay for hosting that will allow you to send high volumes of emails I can see you getting issues if you try and send that many that fast.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上