Announcement

Collapse
No announcement yet.

Is it possible to split an e-mail addressed array into batches ?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possible to split an e-mail addressed array into batches ?

    Hi all,

    I got an application which sends e-mail to an array of users --> $email_to = {email_to} .
    {email_to} can have also more that 2000 addressee
    I would like to insert a sleep(1800) within the routing of 100 email to avoid blocking in e-mail sending but I need to split the array of {email_to} into batches of 100 email at time.
    I don't know how achieve it...

    More or less I have this parameters to use.

    $email_to = {email_to}; // To email array
    $nr_email_to = $count = count($email_to); //Total Nr of email to send
    $nr_mail_batch = ceil($nr_email_to/100); // Nr of batches to send

    sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, "H", $email_to , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);


    Probably it's completly wrong... as approch becouse I'm not a programmer
    Here , I suppose, I need to have another internal loop or similar that send 100 email at time splitting the '$email_to' array correctly otherwise I send all mails everytime ... ;-((

    $x=1;
    while($x<=$nr_mail_batch)
    {
    ??????
    sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, "H", $email_to , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);
    ??????
    sleep(1800)
    $x++;
    }


    if you have a better solution it will be very appreciated
    Googling I saw also array_chunk($array, 100); but I don't know if it could be helpful or not.

  • #2
    Well you could actually do that. Some smtp servers do not allow so any (100) to addresses at once. So you may have to split it in less.
    I personally would do that via phpmailer and have a simple php backgound job running that checks for records in a table. Doing a sleep in php would not be my favorite choice in a web application since it sets a process temporarily on halt (might cause web page timeouts and so on).
    In fact we send bulk mail via phpmailer via a scheduled job. See http://sourceforge.net/projects/phpmailer/
    Then just have a php program that basically works like this (in pseudo code):

    check MailScheduler table for records not having an IsSent set (perform the select * from MailScheduler where IsSent!='Y')
    counter=0;
    for all records do{
    if (counter==100){
    sendmail to userlist;
    if (mail is sent ok){
    update MailScheduler set IsSent='Y' where mailid=userlist
    userlist:=empty;
    }
    sleep 600; //that is 600 seconds
    }
    add user from sqlquery to userlist userlist;
    counter++;
    }
    if (userlist!=empty) {send mail to all userlist);

    Because it is a scheduler job you wont have interference in your scriptcase website. And you can even run it in low priority.

    Comment


    • #3
      Hi,
      array_chunk() should do the trick as well, but rr is right, you might run into trouble with the timing.

      $chunk_size = 100;
      $i = 0;
      $email_to = array_chunk($email_all,$chunk_size);
      while(isset($email_to[$i]))
      {
      sc_mail_send(..., $email_to[$i], ...);
      sleep (600);
      $i ++;
      }


      jsb
      Last edited by jsbinca; 02-26-2014, 01:20 PM.

      Comment


      • #4
        Thanks rr and jsb !!
        Unfortunately to implement a library it's not a piece of cake for me... ;-((
        When you say ' it sets a process temporarily on halt ' means that the user that has run the bulk mailing has to wait until all the mails will be sended or also other user are blocked in their activities ?
        If they send for istance 2000 mails with only text and no attachment , how much could be , more or less, the time the user has to wait ?

        Comment


        • #5
          Hi,

          I implemented the code but I got an error on chunk (string-array) so I added explode

          $email_to_all = {email_to}; {email_to} is a double select field and it's the list of 'all' the to email

          I changed the line to $email_to = array_chunk(explode(';',$email_to_all),$chunk_size ); (I hope it's correct...)

          I did a test (only 3 email addresses just to see if there are errors) but when I click on OK sending button the application goes in connecting an stay there .... forever.
          The only way to exit is to close the browser.


          //Code used

          $chunk_size = 100;
          $i = 0;
          $email_to = array_chunk(explode(';',$email_to_all),$chunk_size );
          while(isset($email_to[$i]))
          {
          sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, "H", $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);
          sleep (20);
          $i ++;
          }
          Last edited by giovannino; 02-27-2014, 08:16 AM.

          Comment


          • #6
            if the php goes to sleep then that part doesnt run further for so many seconds. Meaning the user doesnt get feedback from parts of the page. I would avoid that at all costs therefor I opt for phpmailer. Believe me phpmailer is easy enough to implement. Do a simple search on how to use phpmailer and you will find: http://stackoverflow.com/questions/1...utorial-online

            The user would likely wait for a reply from the server. Yet the reply doesnt come in time since some brwosers expect a reply within x seconds (definitely less then 60) and will thus timeout. THat is not what you would want. In fact the phpmailer option is therefore better.

            Also see: http://phpmailer.worxware.com/?pg=tutorial

            Comment


            • #7
              Just to understand..

              Do I need to add the class under C:\Program Files (x86)\Scriptcase\v71\wwwroot\scriptcase\app\my_app lication\php_mailer ?

              I used include in past but never require : do I use require in the same way of include ----> require("../php_mailer /class.phpmailer.php");

              Comment


              • #8
                that depends where you want to run it. In a seperate job you can simply put the classes in your doc_root dir. Basically for running it from scriptcase you can do the same. The easy way is to put the classes in your php dir..

                Comment


                • #9
                  Hi rr,
                  I did some try and the best that I've reached so far is this error...

                  "Invalid address: 'giovanni.aaaaaa@libero.it;paolo.abcdef@gmail.it;c arlo.jdjdjdj@yahoo.com' You must provide at least one recipient email address. Message Not Sent
                  Mailer Error: You must provide at least one recipient email address."

                  I take the addresses from double select field that output the list in this way: email@xxx.com ;email@yyy.com;.... and so on .
                  'giovanni.aaaaaa@libero.it;paolo.abcdef@gmail.it;c arlo.jdjdjdj@yahoo.com'
                  I really don't know how fix the error
                  Last edited by giovannino; 02-27-2014, 03:17 PM.

                  Comment


                  • #10
                    Hi,
                    there's a blank in the email address.

                    c arlo.jdjdjdj@yahoo.com

                    Comment


                    • #11
                      It 's an editor problem only

                      Unfortunately is a problem of editor here that make strange format when you copy e-mail addresses
                      In realty there are no blank

                      I think that phpmailer wants another kind of string structure itself


                      Originally posted by jsbinca View Post
                      Hi,
                      there's a blank in the email address.

                      c arlo.jdjdjdj@yahoo.com

                      Comment


                      • #12
                        Sorry rr but it's to complex to understand to me.. ;-((
                        How phpmailer expect to be the to addresses ?

                        Comment


                        • #13
                          Hi Jsb,

                          I tested the code and now it work . I know it's not the best of solutions and has risk. Phpmailer could be better but I'm stucked on the error I posted
                          About the sleep(??) . To balance it for 100 mails how much have I to use (more or less...).
                          If I send less than 100 mails the sleep has no effect or I'm wrong.. ??
                          Thanks

                          Comment


                          • #14
                            Hi,
                            you have the sleep at least one time no matter how many emails you have. To skip it on less then 100 you have to count your emails/chunks.

                            $c_count = count($email_to); // number of chuncks

                            In the loop:

                            if($c_count > 1 && $i < $c_count) //skip the sleep if only one batch or the last one
                            {
                            sleep();
                            }

                            jsb

                            Comment


                            • #15
                              This is last version:

                              Is it correct as your suggestion ?

                              $chunk_size = 100;
                              $i = 0;
                              $email_to = array_chunk(explode(';',$email_to_all),$chunk_size );
                              while(isset($email_to[$i]))
                              {
                              if(empty({email_attachment})){
                              sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, "H", $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl]);
                              }
                              else{
                              sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, "H", $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);
                              }
                              if($c_count > 1 && $i < $c_count) //skip the sleep if only one batch or the last one
                              {
                              sleep();
                              }
                              else {
                              sleep (600);
                              {
                              $i ++;
                              }

                              Comment

                              Working...
                              X