0

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
    I'd contact HostGator, it's quite possible they started to block you from sending after that many e-mails. Especially if you're using shared hosting. – Styphon May 14 '14 at 15:23
  • generally a bad idea - sending 38k+ emails is going to take time, and you'll probably exceed the max_execution_time set by hostgator. YOu can certainly TRIGGER the process via an ajax call, but you should use other methods of actually doing the sending. e.g. fork off a process, invoke a background shell process, etc.. anything that'd disassociate the actual sending stuff from the web front-end. that backend can update a table/session var that you monitor via the webfront-end – Marc B May 14 '14 at 15:26
  • I concur with Styphon. They probably think you're a spammer. You're probably better off looking at a dedicated service like Mailchimp. – Matthew Daly May 14 '14 at 15:26
  • The thing here is that I commented the SEND process, like it wasn't actually sending the emails. I just wanted to look at how the process works. But yeah I've been reading a lot of posts about mass mailing, it's a pain in the ass to do it yourself. Thanks tho =) – Brent Heftye May 14 '14 at 15:38

3 Answers3

0

I suspect the max_execution_time

How to increase maximum execution time in php

Could also be the host restraining such behavior.

Community
  • 1
  • 1
Musk
  • 1,477
  • 1
  • 15
  • 25
0

The most common error I could think of is that your script reached maximum execution time, or the script was not made to continue working if browser connection is lost.

Your best bet would be to not have one execution send all the mails, but run your script every 5 minutes and make it send 1500 (5mails per second if you sleep .2sec for each, time 5 minutes). Keep track of what has been sent so the script re-executing will continue where it left off.

Salketer
  • 14,263
  • 2
  • 30
  • 58
0

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.

Ian Davis
  • 109
  • 3
  • Thanks Ian this was a helpful answer. I may have to send 500 per hour, which would take me around 3 to 4 days. – Brent Heftye May 14 '14 at 15:50
  • tell me about it - the send I did ran for several days, luckily it was a one off send out we needed to do. If this is a regular thing you will need to do you should look into specialist hosting for this or your host will be angry if you get their server blacklisted! – Ian Davis May 14 '14 at 16:04