Location: PHPKode > projects > Esvon Personals > esvon-personals/modules/Mailing_Lists/cron.php
<?php

// --------------------------------------------------------------------------
//
// Esvon Classifieds v.4.0
// Copyright(C), Esvon LTD, 2001-2010, All Rights Reserved.
// E-mail: hide@address.com
//
// All forms of reproduction, including, but not limited to, internet posting, 
// printing, e-mailing, faxing and recording are strictly prohibited.
// One license required per site running Esvon Classifieds. 
// To obtain a license for using Esvon Classifieds, please register at
// http://www.esvon.com/pg/products/p_classifieds/
//
// --------------------------------------------------------------------------

function Mailing_Lists_Run(){
global $db;

$ML_MOD = 'Mailing_Lists';

$ML_PFX = TBL_PREFIX.strtolower($ML_MOD);

$TBL_ML = $ML_PFX.'_list';
$TBL_ML_IDX = $ML_PFX.'_idx';
$TBL_ML_LOG = $ML_PFX.'_log';
$TBL_ML_PENDING = $ML_PFX.'_pending';
$TBL_ML_TEMP = $ML_PFX.'_temp';

$MAIL_PER_TIME = 100;
$TIME_WAIT = 60; // seconds

########## MAIN PROGRAM ##########

include_once SITE_PATH.'modules/'.$ML_MOD.'/hw_api.php'; // ML_getEmailsAmountSQL function

$res = $db->query('SELECT id,list_id,subj,content,is_html,extra,status FROM '.$TBL_ML_LOG.' WHERE date<='.SQL_NOW.' AND status < 2');
if(mysql_num_rows($res)<1) return; // no matching mailing lists

$O_UFS = &Factory::singleton('FieldsSet', '{user_fields}');

while($v = mysql_fetch_assoc($res)){

  if(!$v['status']){ 

    if($v['list_id']>0) $ql = 'SELECT '.$v['id'].',email FROM '.$TBL_ML.' WHERE list_id='.$v['list_id'];
    else{
      $ql = ML_getEmailsAmountSQL($v['list_id']);
      $ql = preg_replace('/COUNT\(.*?\)\s/','DISTINCT '.$v['id'].',u.email ',$ql).$v['extra'];
    }

    $db->query('DELETE FROM '.$TBL_ML_TEMP.' WHERE log_id='.$v['id']);
    $db->query('INSERT IGNORE INTO '.$TBL_ML_TEMP.' (log_id,email) '.$ql);
    $db->query('UPDATE '.$TBL_ML_LOG.' SET status=1,amount=0 WHERE id='.$v['id']);
  }

  $unsub_url_tpl = SITE_URL.'modules.php?mod='.$ML_MOD.'&mact=ml&act=unsubscribe&mlist_id='.$v['list_id'].'&email=';

  $total = 0;
  $cur = 0;

  while(1){

  	$z = $db->one_assoc('SELECT email FROM '.$TBL_ML_TEMP.' WHERE log_id='.$v['id'].' LIMIT 1');
  	if(!$z) break;
    $db->query('DELETE FROM '.$TBL_ML_TEMP.' WHERE log_id='.$v['id'].' AND email=?', array($z['email']) );

  	if(IsEmail($z['email'])){

      $a_User = $db->one_assoc('SELECT * FROM '.TBL_USER.' WHERE email=?', array($z['email']) );
      if(!empty($a_User)){ 
        
        $z['userid'] = $a_User['id'];
        $a_User = $O_UFS->FillCustomFieldsArray($a_User, true);
        $O_UFS->AdjustCustomFields($a_User);
        $z += $a_User;
        $a_User = NULL;

        $z['item_id'] = $db->one_data('SELECT link_id FROM '.TBL_AD.' WHERE userid="'.$z['userid'].'" ORDER BY link_id LIMIT 1');
        $z['item_url'] = $z['item_id'] ? SITE_URL.hwGetAdURL($z['item_id']) : '';
      }
      else $z['userid'] = 0; // ##userid## can be used to distinguish

      $z['unsub_url'] = $unsub_url_tpl.urlencode($z['email']);
	
  	  hwSendMail($z['email'],ADMIN_EMAIL,$v['subj'],EvalBuffer($v['content'],$z));
	    $total++;
    }

    $db->query('UPDATE '.$TBL_ML_LOG.' SET amount=amount+1 WHERE id='.$v['id']);

    $cur++;
    if($cur>=$MAIL_PER_TIME){
      $cur = 0;
      if($TIME_WAIT>0) sleep($TIME_WAIT);
    }
    	
  }
	
  $db->query('UPDATE '.$TBL_ML_LOG.' SET status=2 WHERE id='.$v['id']);

  hwSendMail(ADMIN_EMAIL,ADMIN_EMAIL,$v['subj'],"ML #{$v['id']} ($total sent) : {$v['subj']}");
	
}

}
Return current item: Esvon Personals