How to make this script send only 500 emails per hour?

Discussion in 'PHP' started by rucanunes, Apr 26, 2013.

  1. #1
    Hello,

    Can someone please help me make this script send only 500 email per hour?

    <?php
     
    include("include/config.php");
    include("include/functions/import.php");
    $baseurl = $config['baseurl'];
    $purl = $config['purl'] ;
    $sendername = $config['site_name'];
     
    $msg = "<span style='font-family: arial,helvetica,sans-serif;'><a rel='nofollow' target='_blank' href='".$baseurl."'><img alt='' src='".$baseurl."/images/scriptolution.png' style='width: 190px; height: 58px;'></a></span><br/><span style='font-family: arial,helvetica,sans-serif;'><strong>Bem-vindo(a) &#224; edi&#231;&#227;o desta semana da Newsletter 'Tarefas da Semana'!</strong></span><hr><span style='font-family: arial,helvetica,sans-serif;'><u>As seguintes tarefas foram pedidas pelos nossos membros:</u></span>";
    $wantsquery = "SELECT m.username,w.want FROM members m,wants  w where m.USERID=w.USERID order by w.time_added desc limit 10";
    $executequery = $conn->Execute($wantsquery);
    $results= $executequery->getrows();
    for($i=0; $i<count($results);$i++)
    {
        $username = $results[$i]['username'];
        $want = $results[$i]['want'];
        $msg.="<div style='font: 14px arial,helvetica,sans-serif; margin: 4px 0pt 3px;'><a rel='nofollow' style='text-decoration: underline; color: rgb(62, 99, 113);' target='_blank' href='".$baseurl."/conversations/".$username."'>".$username."</a><span style='color: rgb(62, 99, 113);'> precisa de: </span><span style='color: rgb(85, 85, 85);'>".$want."</span></div>";   
    }
     
    $msg.="<br/><br/><span style='font-family: arial,helvetica,sans-serif;'><u>Veja estas tarefas recentes:</u></span><table style='font: 14px arial,helvetica,sans-serif; color: rgb(85, 85, 85); width: 610px; background-color: rgb(255, 255, 255);' cellpadding='0' cellspacing='0'><tbody>";
     
    $gigsquery = "SELECT A.*, B.seo, C.username, C.country from posts A, categories B, members C where A.active='1' AND A.category=B.CATID AND A.USERID=C.USERID order by A.PID desc limit 5";
    $executequery = $conn->Execute($gigsquery);
    $result= $executequery->getrows();
    for($i=0; $i<count($result);$i++)
    {
        $PID = $result[$i]['PID'];
        $gtitle = stripslashes($result[$i]['gtitle']);
        $seo = $result[$i]['seo'];
       
        $title = str_replace(array(":", ".", "^", "*", ",", ";", "~", "[", "]", "<", ">", "\\", "/", "=", "+", "%"),"", $gtitle);
        $last = substr($title, -1);
        if($last == "-")
        {
            $title = substr($title, 0, -1);
        }
        $title = str_replace(" ", "-", $title);
       
        $link = $baseurl . '/' . $seo . '/' . $PID . '/' . $title . '';
       
        $picurl = $purl . '/t2/' . $result[$i]['p1'];   
       
        $msg.="<tr><td style='border-top: 1px dotted rgb(224, 224, 224) ! important; padding: 6px 0pt;'><div style=''><div style='float: left; min-height: 72px; width: 90px;'><a rel='nofollow' target='_blank' href='".$link."'><img src='".$picurl."' alt='".$link."' border='0'></a></div></div></td><td style='border-top: 1px dotted rgb(224, 224, 224) ! important; padding: 6px 0pt;'><div style='float: left; padding-left: 5px;'><div style='padding-bottom: 3px;'><a rel='nofollow' style='color: rgb(8, 112, 142); font-size: 16px; font-weight: bold;' target='_blank' href='".$link."'>Eu vou ".mb_strimwidth($result[$i]['gtitle'],0,50,'...','UTF-8')." por ".$result[$i]['price']."</a></div><div>".mb_strimwidth($result[$i]['gdesc'],0,140,'...','UTF-8')."</div><div style='padding-top: 10px; color: rgb(0, 149, 192);'><div style='float: right; color: rgb(153, 153, 153);'><span style='color: rgb(153, 153, 153); font: 0.834em/1.6em Helvetica,Arial,sans-serif;'>(por <a rel='nofollow' style='text-decoration: underline; color: rgb(153, 153, 153);' target='_blank' href='".$baseurl."/user/".$result[$i]['username']."'>".mb_strimwidth($result[$i]['username'],0,10,'...','UTF-8')."</a>)</span>&#32;<a rel='nofollow' style='font: 0.834em/1.6em Helvetica,Arial,sans-serif; color: rgb(0, 149, 192);' target='_blank' href='".$link."'><span>comprar!</span></a></div><div style='clear: both;'></div></div></div><div style='clear: both;'></div></td></tr>";
    }
     
    $msg.="</tbody></table><br/><br/><span style='font-family: arial,helvetica,sans-serif;'><u>Estas tarefas receberam a melhor classifica&#231;&#227;o a semana passada:</u></span><table style='font: 14px arial,helvetica,sans-serif; color: rgb(85, 85, 85); width: 610px; background-color: rgb(255, 255, 255);' cellpadding='0' cellspacing='0'><tbody>";
     
    $gigsquery = "SELECT distinct A.*, B.seo, C.username, C.country from posts A, categories B, members C, ratings D where A.active='1' AND A.category=B.CATID AND A.USERID=C.USERID and A.PID=D.PID and D.good='1' order by A.feat desc, D.time_added desc limit 5";
    $executequery = $conn->Execute($gigsquery);
    $result= $executequery->getrows();
    for($i=0; $i<count($result);$i++)
    {
        $PID = $result[$i]['PID'];
        $gtitle = stripslashes($result[$i]['gtitle']);
        $seo = $result[$i]['seo'];
       
        $title = str_replace(array(":", ".", "^", "*", ",", ";", "~", "[", "]", "<", ">", "\\", "/", "=", "+", "%"),"", $gtitle);
        $last = substr($title, -1);
        if($last == "-")
        {
            $title = substr($title, 0, -1);
        }
        $title = str_replace(" ", "-", $title);
       
        $link = $baseurl . '/' . $seo . '/' . $PID . '/' . $title . '';
       
        $picurl = $purl . '/t2/' . $result[$i]['p1'];   
       
        $msg.="<tr><td style='border-top: 1px dotted rgb(224, 224, 224) ! important; padding: 6px 0pt;'><div style=''><div style='float: left; min-height: 72px; width: 90px;'><a rel='nofollow' target='_blank' href='".$link."'><img src='".$picurl."' alt='".$link."' border='0'></a></div></div></td><td style='border-top: 1px dotted rgb(224, 224, 224) ! important; padding: 6px 0pt;'><div style='float: left; padding-left: 5px;'><div style='padding-bottom: 3px;'><a rel='nofollow' style='color: rgb(8, 112, 142); font-size: 16px; font-weight: bold;' target='_blank' href='".$link."'>Eu vou ".mb_strimwidth($result[$i]['gtitle'],0,50,'...','UTF-8')." for ".$result[$i]['price']."</a></div><div>".mb_strimwidth($result[$i]['gdesc'],0,140,'...','UTF-8')."</div><div style='padding-top: 10px; color: rgb(0, 149, 192);'><div style='float: right; color: rgb(153, 153, 153);'><span style='color: rgb(153, 153, 153); font: 0.834em/1.6em Helvetica,Arial,sans-serif;'>(por <a rel='nofollow' style='text-decoration: underline; color: rgb(153, 153, 153);' target='_blank' href='".$baseurl."/user/".$result[$i]['username']."'>".mb_strimwidth($result[$i]['username'],0,10,'...','UTF-8')."</a>)</span><a rel='nofollow' style='font: 0.834em/1.6em Helvetica,Arial,sans-serif; color: rgb(0, 149, 192);' target='_blank' href='".$link."'><span>order now!</span></a></div><div style='clear: both;'></div></div></div><div style='clear: both;'></div></td></tr>";
    }
     
    $msg.="</tbody></table><br/><br/><span style='font-family: arial,helvetica,sans-serif;'>Esperamos que tenham gostado da edi&#231;&#227;o desta semana!<br> Envie-nos as suas quest&#245;es, coment&#225;rios ou ideias em como tornar o ".$sendername." ainda melhor!</span><br/><br/><span style='font-family: arial,helvetica,sans-serif;'>Bons neg&#243;cios,</span><br/><span style='font-family:arial,helvetica,sans-serif;'>A equipa do ".$sendername."</span><br/><br/><span style='font-size: 9px;'><span style='font-family: arial,helvetica,sans-serif;'>Este email n&#227;o &#233; SPAM. Est&#225; a receber este email porque &#233; um membro registado no <a target='_blank' href='".$baseurl."'>".$baseurl."</a></span></span>";
           
     
    $from = $config['site_email'];
    $subject = $config['site_name']." apresenta: Tarefas da semana";
    //Comment the below line
    //mailme('geral@mais5.com',$sendername,$from,$subject,$msg,$bcc);
    //Uncomment the below lines
    $query2 = "select username,email from members where status=1";
    $executequery = $conn->Execute($query2);
    $results= $executequery->getrows();
    for($i=0; $i<count($results);$i++)
    {
      $sendto = $results[$i]['email'];
      mailme($sendto,$sendername,$from,$subject,$msg,$bcc);
    }
    ?>
    Code (markup):
    Thank you!
    Raul Nunes
     
    rucanunes, Apr 26, 2013 IP
  2. Devtard

    Devtard Notable Member

    Messages:
    850
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    220
    #2
    Hi, I won't bother to read your code (since you didn't even tell us what exactly it is doing) but here is a possible solution:

    • With every execution of the script, save the amount of already sent emails (and the last used email address or its ID) to a database.
    • If the number is higher than 500, don't execute the code.
    • Reset the number every hour.
    • When the last used email equals the last email in the database, don't execute the code.
     
    Devtard, Apr 26, 2013 IP
  3. rucanunes

    rucanunes Active Member

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Sorry about that!!
    This is part of a Fiverr like script. This php file sends emails to all the members registered in the site with the "Gigs of the Week". Problem is that my hosting only allows 500 emails per hour and this script does not limit that.
    Any help would be greatly appreciated!!
     
    rucanunes, Apr 27, 2013 IP
  4. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #4
    It's better to use cron jobs to send emails and scheduling.
    But you can try this:

    1. Add this function to your code:
    function is_allowed()
    {
        if (file_exists('counter.txt') === FALSE)
            file_put_contents('counter.txt', '0-' . time());
     
        $data = file_get_contents('counter.txt');
        list($count, $last_time) = explode('-', $data);
     
        if ($last_time + 3600 > time())
        {
            $count = 0;
            $last_time = time();
        }
     
        if ($count < 500)
            $count++;
        else
            return FALSE;
     
        return file_put_contents('counter.txt', $count . '-' . $last_time);
    }
    PHP:
    2. Add if statement in the for loop
    for($i=0; $i<count($results);$i++)
    {
        if (is_allowed() === FALSE)
            break;
     
        $sendto = $results[$i]['email'];
        mailme($sendto,$sendername,$from,$subject,$msg,$bcc);
    }
    PHP:
     
    Hamidsam, Apr 29, 2013 IP