4 * Massmail backend cron script
5 * This is mass mailing backend script which actually sends messages
6 * of the mailings scheduled via the web frontend.
7 * Mailing types, for which this is applicable, have trailer
8 * appended with individual URL for unsubscription from future
11 * Copyright 1999-2001 (c) VA Linux Systems
12 * Copyright 2003 (c) GForge, LLC
16 * This file is part of GForge.
18 * GForge is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * GForge is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with GForge; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
33 require dirname(__FILE__).'/../www/env.inc.php';
34 require $gfwww.'include/squal_pre.php';
35 require $gfcommon.'include/cron_utils.php';
39 if (!cron_create_lock(__FILE__)) {
40 $err = "Massmail already running...exiting";
41 if (!cron_entry(6,$err)) {
42 # rely on crond to report the error
43 echo "cron_entry error: ".db_error()."\n";
48 // Pause between messages, sec
51 // This tables maps mailing types to tables which required to perform it
52 $table_mapping = array(
56 'DVLPR' => "users,user_group",
57 'ADMIN' => "users,user_group,groups",
58 'SFDVLPR' => "users,user_group",
61 // This tables maps mailing types to WHERE subclauses which select
63 $cond_mapping = array(
65 'SITE' => "AND mail_siteupdates=1",
66 'COMMNTY' => "AND mail_va=1",
67 'DVLPR' => "AND users.user_id=user_group.user_id",
68 'ADMIN' => "AND users.user_id=user_group.user_id AND user_group.admin_flags='A' AND groups.status='A' AND groups.group_id=user_group.group_id",
69 'SFDVLPR' => "AND users.user_id=user_group.user_id AND user_group.group_id=1"
72 $mail_res = db_query("SELECT *
75 ORDER BY queued_date");
77 /* If there was error, notify admins, but don't be pesky */
79 $err .= "cannot execute query to select pending mailings: ".db_error()."\n";
80 $hrs = time()/(60*60);
81 // Send reminder every second day at 11am
82 if (($hrs%24)==11 && (($hrs/24)%2)==1) {
83 global $sys_admin_email;
86 "ATT: Problems with massmail cron script",
87 "This is automatically generated message from\n"
88 ."the mass mailing cron script of $sys_name\n"
89 ."installation. There was error querying massmail_queue\n"
90 ."database table. Please take appropriate actions.\n"
96 // $err .= "Got ".db_numrows($mail_res)." rows\n";
98 if (db_numrows($mail_res)<1) {
103 $type = db_result($mail_res, 0, 'type');
104 if (!$table_mapping[$type]) {
105 $err .= "Unknown mailing type\n";
109 $subj = db_result($mail_res, 0, 'subject');
110 $mail_id = db_result($mail_res, 0, 'id');
112 //$err .= "Got mail to send: ".$subj."\n";
114 $sql = "SELECT DISTINCT users.user_id,users.user_name,users.realname,users.email,users.confirm_hash
115 FROM $table_mapping[$type]
116 WHERE users.user_id>".db_result($mail_res, 0, 'last_userid')."
118 ".$cond_mapping[$type]."
119 ORDER BY users.user_id";
123 // Get next chunk of users to mail
124 $users_res = db_query($sql);
126 $err .= "Mailing ".db_numrows($users_res)." users.\n";
128 // If no more users left, we've finished with this mailing
129 if ($users_res && db_numrows($users_res)==0) {
130 db_query("UPDATE massmail_queue
131 SET failed_date=0,finished_date='".time()."'
132 WHERE id='$mail_id'");
136 // Actual mailing loop
138 while ($row =& db_fetch_array($users_res)) {
140 if ($type=='SITE' || $type=='COMMNTY') {
141 $tail = "\r\n==================================================================\r\n" ;
142 $tail .= sprintf (_('You receive this message because you subscribed to %1$s
143 site mailing(s). You may opt out from some of them selectively
144 by logging in to %1$s and visiting your Account Maintenance
145 page (%2$s), or disable them altogether
146 by visiting following link:
149 $GLOBALS['sys_name'],
150 util_make_url('/account/'),
151 util_make_url('/account/unsubscribe.php?ch=_'.$row['confirm_hash'])) ;
155 util_send_message($row['email'],$subj, $body."\r\n".$tail,'noreply@'.$sys_default_domain);
156 $last_userid = $row['user_id'];
161 $sql="UPDATE massmail_queue
163 last_userid='$last_userid',
164 finished_date='".time()."'
165 WHERE id='$mail_id'";
170 $err .= $sql.db_error();
172 $mess = "massmail $compt mails sent";
175 function m_exit($mess = '') {
178 if (!cron_remove_lock(__FILE__)) {
179 $err .= "Could not remove lock\n";
181 if (!cron_entry(6,$mess.$err)) {
182 # rely on crond to report the error
183 echo "cron_entry error: ".db_error()."\n";
190 // c-file-style: "bsd"