/ tasks / pass_mailer.php
pass_mailer.php
  1  <?php
  2  require_once 'lib/db.php';
  3  
  4  set_time_limit(0);
  5  
  6  ini_set("memory_limit", "-1");
  7  
  8  define(SLEEP_TIME, 3);
  9  
 10  function send_email($email, $token, $pending_id, $reminder_interval, $expires_on, $isExpired) {
 11    // Subject / Message
 12    if ($isExpired) {
 13      $subject = "Your 4chan Pass has expired!";
 14      $message =<<<MSG
 15  Your 4chan Pass (Token: $token) has expired.
 16  
 17  In order to continue posting without typing a CAPTCHA, you must renew your Pass. Renewing your 
 18  Pass will add 12 additional months from the date of your renewal payment.
 19  
 20  You can renew your Pass by visiting the following link: https://www.4chan.org/pass?renew=$pending_id
 21  
 22  If you have any questions or problems renewing, please e-mail 4chanpass@4chan.org
 23  
 24  Thanks for your support!
 25  MSG;
 26    }
 27    else {
 28      $subject = "Your 4chan Pass is about to expire";
 29      $message =<<<MSG
 30  Your 4chan Pass (Token: $token) is due to expire in less than $reminder_interval days, on $expires_on.
 31  
 32  To avoid any interruption, we recommend renewing your Pass now. Renewing your Pass will add 12 
 33  additional months to your current expiration date.
 34  
 35  You can renew your Pass by visiting the following link: https://www.4chan.org/pass?renew=$pending_id
 36  
 37  If you have any questions or problems renewing, please e-mail 4chanpass@4chan.org
 38  
 39  Thanks for your support!
 40  MSG;
 41    }
 42    
 43    // From:
 44    $headers = "From: 4chan Pass <4chanpass@4chan.org>\r\n";
 45    $headers .= "MIME-Version: 1.0\r\n";
 46    $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
 47    
 48    // Envelope
 49    $opts = '-f 4chanpass@4chan.org';
 50    
 51    return mail($email, $subject, $message, $headers, $opts);
 52  }
 53  
 54  // Remindre interval in days
 55  $reminder_interval = 7;
 56  
 57  echo '### MAILER BEGIN RUN AT ' . date('r') . " ###\n";
 58  
 59  $query =<<<SQL
 60  SELECT *,
 61  UNIX_TIMESTAMP(expiration_date) as expiration_timestamp,
 62  DATE_FORMAT(expiration_date, '%m/%d/%y') as expires_on
 63  FROM pass_users
 64  WHERE expiration_date <= DATE_ADD(NOW(), INTERVAL $reminder_interval DAY)
 65  AND (email_expired_sent = 0 OR email_reminder_sent = 0)
 66  AND (status = 0 OR status = 6)
 67  SQL;
 68  
 69  $res = mysql_global_call($query);
 70  
 71  if (!$res) {
 72    die('Database error');
 73  }
 74  
 75  if (mysql_num_rows($res) < 1) {
 76    die('Nothing to do');
 77  }
 78  
 79  $i = 0;
 80  $expiration_count = 0;
 81  $reminder_count = 0;
 82  
 83  while ($row = mysql_fetch_assoc($res)) {
 84    if (!$row) {
 85      break;
 86    }
 87    
 88    if ($row['gift_email'] !== '') {
 89      $owner_email = $row['gift_email'];
 90    }
 91    else {
 92      $owner_email = $row['email'];
 93    }
 94    
 95    if ($row['email_expired_sent'] == '1') {
 96      echo "Nothing to do for $owner_email (already expired)\n";
 97      continue;
 98    }
 99    
100    if ((int)$row['expiration_timestamp'] <= time()) {
101      $isExpired = true;
102    }
103    else if ($row['email_reminder_sent'] != '1') {
104      $isExpired = false;
105    }
106    else {
107      echo "Nothing to do for $owner_email\n";
108      continue;
109    }
110    
111    $status = send_email($owner_email, $row['user_hash'], $row['pending_id'], $reminder_interval, $row['expires_on'], $isExpired);
112    
113    if ($status) {
114      if ($isExpired) {
115        ++$expiration_count;
116        echo "Sent expiration notice to $owner_email\n";
117        $query = "UPDATE pass_users SET email_expired_sent = 1, status = 1, last_status = status WHERE pending_id = '" . $row['pending_id'] . "' LIMIT 1";
118      }
119      else {
120        ++$reminder_count;
121        echo "Sent reminder to $owner_email\n";
122        $query = "UPDATE pass_users SET email_reminder_sent = 1 WHERE pending_id = '" . $row['pending_id'] . "' LIMIT 1";
123      }
124      
125      mysql_global_call($query);
126    }
127    else {
128      echo "mail error $owner_mail";
129    }
130    
131    ++$i;
132    
133    sleep(SLEEP_TIME);
134  }
135  
136  echo "---------------------------------\n";
137  echo "Sent $i email(s): $reminder_count reminder(s), $expiration_count expiration(s)\n";
138  echo '### MAILER END RUN AT ' . date('r') . " ###\n";