Menu

Changing Email Send Rate

Help
Jeff
2012-11-16
2012-11-17
  • Jeff

    Jeff - 2012-11-16

    So i recently purchased a daily deal clone that sends the automated daily emails throught SMTP using PHPmailer and Chron Jobs.. My problem is that me email list has grown so large that my host (Hostgator) only allows 500 emails/hour and all me automated emails are not going out... I have gone through the scripts and found they have script fot SMTP that then points to PHPMailer. I need help slowing down the emails so that they all go out (but send at a rate less than 500 per hour)...

    Here is the Code...

    <?php
    //error_reporting(E_ALL);
    //error_reporting(E_STRICT);
    date_default_timezone_set('America/Los_Angeles');
    require_once('class.phpmailer.php');
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    
    $mail             = new PHPMailer();
    
    //$body             = file_get_contents('contents.html');
    
    $body             = $message;
    $body             = eregi_replace("[\]",'',$body);
    
    $mail->IsSMTP(); // telling the class to use SMTP
    
    // $mail->SMTPDebug  = 2;                   
    // enables SMTP debug information (for testing)
    // 1 = errors and messages
    // 2 = messages only
    
    $mail->SMTPAuth   = true;                           // enable SMTP authentication
    $mail->SMTPSecure = SMTP_TRANSPORT_LAYER_SECURITY;  // sets the prefix to the server
    $mail->Host       = SMTP_HOST;                      // sets SMTP server HOST name
    $mail->Port       = SMTP_PORT;                      // set the SMTP port for the server
    $mail->Username   = SMTP_USERNAME;                 // SMTP username
    $mail->Password   = SMTP_PASSWORD;                 // SMTP password
    
    $mail->SetFrom(FROM_EMAIL, FROM_NAME);
    
    $mail->AddReplyTo(REPLY_TO_EMAIL,REPLY_TO_NAME);
    
    $mail->Subject    = $subject;
    
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    //$mail->AddAddress(SITE_EMAIL, APP_NAME);
    
    $to_list = explode(',',$to);
    
    $between_delay = 75; //max limit of mails send at a slot
    $send_count = 1; 
    $send_delay = 1; //Delays the program execution for the given number of seconds.
    
    ignore_user_abort(true); // Ignore user aborts and allow the script to run forever
    set_time_limit(300); //to prevent the script from dying
    
    foreach($to_list as $row)
    {
    
        if ( ($send_count % $between_delay) == 0 ){
            sleep( $send_delay ); //Delays the program execution for the given number of seconds.
        }
        $address = $row;
        if(!empty($address)) {
            $mail->AddAddress($address, "User");
            $mail->Send();  
            $mail->ClearAddresses(); //clear address
        }
    $send_count++;
    
    }
    
    if(!empty($mail->ErrorInfo)) { 
    
          set_response_mes(-1,"Mailer Error: " . $mail->ErrorInfo);
          if($_SESSION['userrole'] == 1)
          {
            $url = substr($_SERVER['REQUEST_URI'],1); 
            if($url=='admin/daily_mails/' || $url=='admin/daily_mails.php/'){ 
                $url='admin/profile/'; 
            }
            url_redirect(DOCROOT.$url);
          }
          url_redirect(DOCROOT);
    
    }
    
    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    
    ?>
    

    I am pretty sure I need to change these numbers, but i am not sure which number represents what...

    $between_delay = 75;
    $send_count = 1;
    $send_delay = 1;

    Any help or insight you can provide would be greatly, greatly appreciated.

     
    • Marcus Bointon

      Marcus Bointon - 2012-11-16

      On 16 Nov 2012, at 21:53, "Jeff McLean" c7m4d4@users.sf.net wrote:

      foreach($to_list as $row)
      {

      if ( ($send_count % $between_delay) == 0 ){
          sleep( $send_delay ); //Delays the program execution for the given number of seconds.
      }
      $address = $row;
      if(!empty($address)) {
          $mail->AddAddress($address, "User");
          $mail->Send();  
          $mail->ClearAddresses(); //clear address
      }
      

      $send_count++;

      }
      I am pretty sure I need to change these numbers, but i am not sure which number represents what...
      $between_delay = 75;
      $send_count = 1;
      $send_delay = 1;

      You can see where these numbers are used in the code above. Every $between_delay messages, it sleeps for $send_delay seconds. So to limit to 500/hour, you could wait for ceil(3600/500) = 8 seconds between each message, so set $between_delay to 1 and $send_delay to 8.

      This does mean that 500 messages will take an hour to send, but that means you don't need to worry about stopping other things from happening. You may need to increase your time limit too, though it should be infinite for CLI scripts anyway.

      A much better way of doing this would be to have a rate-limiting queue that you push your messages into as fast as you can create them, and then pull them out at a steady rate from another process. That way you can have multiple generators and not have to worry about the sum of their output rates. Take a look at beanstalkd or zeromq.

      FWIW, I use PHPMailer to send up to about 2 million messages/hour.

      Marcus

      Marcus Bointon
      Synchromedia Limited: Creators of http://www.smartmessages.net/
      UK info@hand CRM solutions
      marcus@synchromedia.co.uk | http://www.synchromedia.co.uk/

       
    • Patrick Chuprina

      Unsubscribe

      On Friday, November 16, 2012, Jeff McLean wrote:

      So i recently purchased a daily deal clone that sends the automated daily
      emails throught SMTP using PHPmailer and Chron Jobs.. My problem is that me
      email list has grown so large that my host (Hostgator) only allows 500
      emails/hour and all me automated emails are not going out... I have gone
      through the scripts and found they have script fot SMTP that then points to
      PHPMailer. I need help slowing down the emails so that they all go out (but
      send at a rate less than 500 per hour)...

      Here is the Code...

      wzxhzdk:0
      $mail = new PHPMailer();
      //$body = file_get_contents('contents.html');
      $body = $message;$body = eregi_replace("[]",'',$body);
      $mail->IsSMTP(); // telling the class to use SMTP
      // $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)// 1 = errors and messages// 2 = messages only
      $mail->SMTPAuth = true; // enable SMTP authentication$mail->SMTPSecure = SMTP_TRANSPORT_LAYER_SECURITY; // sets the prefix to the server$mail->Host = SMTP_HOST; // sets SMTP server HOST name$mail->Port = SMTP_PORT; // set the SMTP port for the server$mail->Username = SMTP_USERNAME; // SMTP username$mail->Password = SMTP_PASSWORD; // SMTP password
      $mail->SetFrom(FROM_EMAIL, FROM_NAME);
      $mail->AddReplyTo(REPLY_TO_EMAIL,REPLY_TO_NAME);
      $mail->Subject = $subject;
      $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
      $mail->MsgHTML($body);
      //$mail->AddAddress(SITE_EMAIL, APP_NAME);
      $to_list = explode(',',$to);
      $between_delay = 75; //max limit of mails send at a slot$send_count = 1; $send_delay = 1; //Delays the program execution for the given number of seconds.
      ignore_user_abort(true); // Ignore user aborts and allow the script to run foreverset_time_limit(300); //to prevent the script from dying
      foreach($to_list as $row){

      if ( ($send_count % $between_delay) == 0 ){
          sleep( $send_delay ); //Delays the program execution for the given number of seconds.
      }
      $address = $row;
      if(!empty($address)) {
          $mail->AddAddress($address, "User");
          $mail->Send();
          $mail->ClearAddresses(); //clear address
      }$send_count++;
      

      }
      if(!empty($mail->ErrorInfo)) {

        set_response_mes(-1,"Mailer Error: " . $mail->ErrorInfo);
        if($_SESSION['userrole'] == 1)
        {
          $url = substr($_SERVER['REQUEST_URI'],1);
          if($url=='admin/daily_mails/' || $url=='admin/daily_mails.php/'){
              $url='admin/profile/';
          }
          url_redirect(DOCROOT.$url);
        }
        url_redirect(DOCROOT);
      

      }
      //$mail->AddAttachment("images/phpmailer.gif"); // attachment//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
      ?>

      I am pretty sure I need to change these numbers, but i am not sure which
      number represents what...

      $between_delay = 75;
      $send_count = 1;
      $send_delay = 1;

      Any help or insight you can provide would be greatly, greatly appreciated.

      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/phpmailer/discussion/81620/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/prefs/

      --

      Fine web design and great photography

      215 Main Street, P.O. Box 135
      Schomberg, ON
      L0G 1T0

      http://www.chuprinacreative.com
      http://www.classiccargallery.ca
      T: 905.939.2242
      F: 905.939.0859