/', '//', '//', '//', ); $rep = array( $user->name, gmdate('d F Y', $user->create_time), number_format($user->total_credit, 0), URL_BASE."opt_out.php?code=".salted_key($user->authenticator)."&userid=$user->id", ); return preg_replace($pat, $rep, $template); } function mail_type($user, $email_file) { global $testing; if ($email_file['html']) { $html = replace($user, $email_file['html']); } else { $html = null; } $text = replace($user, $email_file['text']); if ($testing) { echo "\nSending to $user->email_addr:\n"; echo "------- SUBJECT ----------\n"; echo $email_file['subject']; echo "\n------- HTML ----------\n"; echo $html; echo "\n------- TEXT ----------\n"; echo $text; } else { send_email( $user, $email_file['subject'], $text, $html ); } } function handle_user($user, $email_files) { if ($user->total_credit == 0) { mail_type($user, $email_files['failed']); } else if ($user->expavg_credit < 1) { mail_type($user, $email_files['lapsed']); } else { mail_type($user, $email_files['current']); } } function do_batch($email_files, $startid, $n, $log) { $result = mysql_query( "select * from user where id>$startid order by id limit $n" ); while ($user = mysql_fetch_object($result)) { handle_user($user, $email_files); $startid = $user->id; fputs($log, '$user->id\n'); fflush($log); } mysql_free_result($result); return $startid; } function read_log() { $f = fopen($logfile, 'r'); if (!$f) { echo '$logfile not found - create empty file and run again\n'; exit(); } $startid = 0; while (fscanf($f, '%d', &$startid)) { } fclose($f); return $startid; } function main($email_files) { $startid = read_log(); $f = fopen($logfile, 'w'); $n = 1000; while (1) { $new_startid = do_batch($email_files, $startid, $n, $f); if ($new_startid == $startid) break; $startid = $new_startid; } echo 'All done!\n'; } function test($email_files) { } if (!$USE_PHPMAILER) { echo "You must use PHPMailer.\n"; exit(); } $email_files = read_email_files(); if ($userid) { $user = lookup_user_id($userid); mail_type($user, $email_files['failed']); mail_type($user, $email_files['lapsed']); mail_type($user, $email_files['current']); } else { main($email_files); } ?>