Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: How to throttle a simple emailer script to limit messages sent per hour

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default My While Loop won't Loop

    Part 1
    I have to send out an email ASAP to 158 people based on certain criteria. The SELECT query works fine when I run it against the database and returns the correct 158 records. To test it, I changed the email address to mine so I could see what gets sent, but I thought I would get 158 emails. Instead I received only one. It seems to be sending to the first record only.

    I can't figure out why my WHILE loop is not looping through all 158 records. The includes should not be causing any problems as the email itself had all the variables filled in correctly. I've been looking at it too long. Can anyone see what I cannot? Thanks!

    Code:
    <?php
    include('cr-connectdb.php');
    require('class.phpmailer.php');
    
    $sql = "SELECT * FROM `booking` WHERE `Book_Date` < '2010-6-30' ";
    $result = mysql_query($sql,$connection) or die("Couldn't execute $sql query"); 
    
    while($row = mysql_fetch_assoc($result)){
      include('load-booking-variables.php');
      $body = file_get_contents($Email_Alamo_Refund);	
      include('replace-email-variables.php');
      
      $Subject = "Notice Regarding Your Hawaii Car Rental";
      $emailaddress = $Email;
      include('send-client-email.php'); 
    }
    ?>
    Last edited by djr33; 09-01-2010 at 01:06 AM. Reason: simplified

  2. #2
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default How to throttle a simple emailer script to limit messages sent per hour

    Part 2
    I'm trying to create a simple custom emailer script that uses class.phpmailer.php to send an html email to client email addresses selected from a database table.

    It is basically a SELECT command to retrieve the email addresses and then a WHILE loop that assembles and sends the email. As I do not know how many messages it sends out per hour by default, how do I throttle the sending to make sure it does not go beyond the host's limit? Is the proper way to use a SLEEP command inside the WHILE loop? Something like sleep(10);? I'm having trouble with the while loop sending to only the first record.

    I would also like to have some way to test the script without sending any emails, such as echoing the email addresses it would send to once I enable the sending.

    Any help would be greatly appreciated. Thanks.
    Last edited by djr33; 09-01-2010 at 01:06 AM.

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    RE: Part 1
    Not sure about the sending...

    I would also like to have some way to test the script without sending any emails, such as echoing the email addresses it would send to once I enable the sending.
    Comment out the mail() line and echo the values instead.
    Last edited by djr33; 09-01-2010 at 01:06 AM.
    Corrections to my coding/thoughts welcome.

  4. The Following User Says Thank You to bluewalrus For This Useful Post:

    kuau (08-31-2010)

  5. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Thanks, bw. OK, I did that and it displayed only one email address. It confirmed what I suspected, that the WHILE loop is stopping after the first record in the array. There are actually 158 records returned from the real SQL command. Can you please tell me what is wrong with my while loop?

    Code:
    $sql = "SELECT * FROM `booking` WHERE `Book_Date` < '2010-6-30' AND `Book_Begin_Date` > '2010-8-31' ";
    $result = mysql_query($sql,$connection) or die("Couldn't execute $sql query."); 
    
    while($row = mysql_fetch_array($result)){
      include('load-booking-variables.php');
      echo $emailaddress;l
    }
    Last edited by kuau; 08-31-2010 at 10:18 PM. Reason: so it makes more sense

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    RE: Part 1
    Replace this line include('send-client-email.php'); with echo $emailaddress; and see what the results look like. That'll give you a list of the emails and you can see if it's all working at this stage at least.
    Last edited by djr33; 09-01-2010 at 01:06 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    RE: Part 2
    It looks like you're matching a specific entry, using the "WHERE" clause like that, but I don't know how your database is setup. Is this the same topic/question as your other post? Should they be merged, or are you working on two similar scripts?
    Last edited by djr33; 09-01-2010 at 01:06 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Hi Daniel: I was hoping you would show up.

    Yes, you can combine the two threads as they are related. I am trying to write a generic emailer script that I can use in any situation. I have been using Pommo but it has a lot of bugs and limitations. The emailing I am trying to do right this moment is a specific application of the generic mailer. As there are only 158 records involved this time, I don't need to throttle it, but I want to know how to do it for the master script. So I am trying to do 2 things at once... create the general from the specific.

    I simplified the code so that a logic error would stand out more. I have successfully used WHILE loops in situations where the results are displayed on a web page. This time the result is to send an email and write to a text file that the email was sent. It works for the first record only. I tried using mysql_fetch_assoc($result), and mysql_fetch_array($result), incrementing a counter, using array_push(), using array_push AND a counter... no matter what I do, it echoes only one email address. I can't figure out what I am doing wrong.

    When I run the real SQL query against the database, it returns the correct 158 records. Shouldn't it echo 158 email addresses when I run the test? This is driving me nuts. [I edited the code above so that it is clearer what I am doing]

    Thanks!
    Last edited by kuau; 08-31-2010 at 10:19 PM. Reason: clarification

  9. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I've merged the two discussions.

    There's nothing wrong with your syntax for the while loop. Assuming the simplified version didn't take any errors out, there is only one possibility (at least as far as I can see). It must be the included file... or at least let's eliminate that before continuing.

    Comment out this line:
    //include('load-booking-variables.php');

    Then fix the echo so that it is echoing the right part of the array $row.
    Example: echo $row['EMAIL'];


    My guess is that there may be a naming conflict (possibly $row or $result) within the included file. Note that included files share scope with the main page and can overwrite a value. For this reason using functions can be a safe way to do it or you can make sure not to use the same names.


    Now getting back to the issue of sending too many emails, the first question is how your host checks this. It is likely a limit of no more than X number of emails in Y time. But that could be no more than 3 per second or no more than 1,000 per month. The approach that makes the most sense will depend on this. If you find that out, you will know the best way to limit it.
    sleep() is a good way to stop the emails from sending too often, but that may cause two problems:
    1. The system may still notice that you are sending a lot of emails from one script or within a short period of time (for example 60 in 1 minute).
    2. Most PHP configurations have a maximum execution time: with a large delay like 10 seconds and possibly a lot of records, you will VERY easily reach the default 30 second limit. It is possible to change this using php.ini, .htaccess or even just a command in the script, but this must be changed before you can use sleep() effectively unless it's for a very short amount of time. Note that to a computer, 100 miliseconds may be plenty of time, though if the host has a limit that will be more relevant. To us, it may seem like pausing 5 seconds is necessary, but for operations like this that is an eternity in digital time.
    Last edited by djr33; 09-01-2010 at 01:15 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (09-01-2010)

  11. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    OK, I did that and the one email address disappeared. I doubt the include has an error because I use it elsewhere with no problem. Plus it did successfully pass the variable values to the email template for the one email that did get sent.

    However, I did make some progress on it myself while I was waiting for you, by using 2 while loops. I got it to list the 158 email addresses and write them to the text file but there is an error message inbetween which increments by 10 each time. What does this mean?...

    Code:
    Warning: mysql_fetch_array(): 16 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    one@hawaiiantel.net
    
    Warning: mysql_fetch_array(): 26 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    carlabbess@comcast.net
    
    Warning: mysql_fetch_array(): 36 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    redtide101@aol.com
    
    Warning: mysql_fetch_array(): 46 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    shortnsassy@hawaii.rr.com
    
    Warning: mysql_fetch_array(): 56 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    jallen31@nc.rr.com
    
    Warning: mysql_fetch_array(): 66 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    paramini@cox.net
    
    Warning: mysql_fetch_array(): 76 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    teresaayala1@hotmail.com
    
    Warning: mysql_fetch_array(): 86 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    teresaayala1@hotmail.com
    
    Warning: mysql_fetch_array(): 96 is not a valid MySQL result resource in /home1/mauiretr/public_html/_car/php/alamo-refund4.php on line 28
    lbaker0127@gmail.com
    This is line 28: while($row = mysql_fetch_array($result)){

    I can get rid of the error message by adding an "@" sign but would like to know why it is there. It doesn't do it for the first record. I'll see if the email still works.
    Last edited by kuau; 09-01-2010 at 01:40 AM. Reason: sp

  12. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's hard to know exactly why that is happening, but I believe that means that $result is set to '16' and the other number at those times. That is... it should be the query result, but it isn't??
    How are you counting every 10 records? That may be how this is working like that.

    As for my example, it may be irrelevant now, but did you adjust "EMAIL" to the right field name? There's no reason, if that is changed, it shouldn't work. If it doesn't, it points to a bigger problem, I believe.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •