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
14 * This file is part of GForge.
16 * GForge is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * GForge is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with GForge; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
31 require dirname(__FILE__).'/../www/env.inc.php';
32 require $gfwww.'include/squal_pre.php';
33 require $gfcommon.'include/cron_utils.php';
37 if (!cron_create_lock(__FILE__)) {
38 $err = "Massmail already running...exiting";
39 if (!cron_entry(6,$err)) {
40 # rely on crond to report the error
41 echo "cron_entry error: ".db_error()."\n";
46 // Pause between messages, sec
49 // This tables maps mailing types to tables which required to perform it
50 $table_mapping = array(
54 'DVLPR' => "users,user_group",
55 'ADMIN' => "users,user_group,groups",
56 'SFDVLPR' => "users,user_group",
59 // This tables maps mailing types to WHERE subclauses which select
61 $cond_mapping = array(
63 'SITE' => "AND mail_siteupdates=1",
64 'COMMNTY' => "AND mail_va=1",
65 'DVLPR' => "AND users.user_id=user_group.user_id",
66 '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",
67 'SFDVLPR' => "AND users.user_id=user_group.user_id AND user_group.group_id=1"
70 $mail_res = db_query_params ('SELECT *
73 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 ".forge_get_config ('forge_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');
111 $body = db_result($mail_res, 0, 'message');
112 //$err .= "Got mail to send: ".$subj."\n";
114 $qpa = db_construct_qpa (false, 'SELECT DISTINCT users.user_id,users.user_name,users.realname,users.email,users.confirm_hash') ;
119 $qpa = db_construct_qpa ($qpa, ' FROM users') ;
123 $qpa = db_construct_qpa ($qpa, ' FROM users, user_group') ;
126 $qpa = db_construct_qpa ($qpa, ' FROM users, user_group, groups') ;
130 $qpa = db_construct_qpa ($qpa, ' WHERE users.user_id > $1 AND users.status=$2',
131 array (db_result($mail_res, 0, 'last_userid'),
134 $cond_mapping = array(
148 $qpa = db_construct_qpa ($qpa, ' AND mail_siteupdates=1') ;
151 $qpa = db_construct_qpa ($qpa, ' AND mail_va=1') ;
154 $qpa = db_construct_qpa ($qpa, ' AND users.user_id=user_group.user_id') ;
157 $qpa = db_construct_qpa ($qpa, ' AND users.user_id=user_group.user_id AND user_group.group_id=1') ;
160 $qpa = db_construct_qpa ($qpa, ' AND users.user_id=user_group.user_id AND user_group.admin_flags=$1 AND groups.status=$2 AND groups.group_id=user_group.group_id',
165 $qpa = db_construct_qpa ($qpa, ' ORDER BY users.user_id') ;
167 // Get next chunk of users to mail
168 $users_res = db_query_qpa ($qpa);
170 $err .= "Mailing ".db_numrows($users_res)." users.\n";
172 // If no more users left, we've finished with this mailing
173 if ($users_res && db_numrows($users_res)==0) {
174 db_query_params ('UPDATE massmail_queue SET failed_date=0,finished_date=$1 WHERE id=$2',
180 // Actual mailing loop
182 while ($row =& db_fetch_array($users_res)) {
184 if ($type=='SITE' || $type=='COMMNTY') {
185 $tail = "\r\n==================================================================\r\n" ;
186 $tail .= sprintf (_('You receive this message because you subscribed to %1$s
187 site mailing(s). You may opt out from some of them selectively
188 by logging in to %1$s and visiting your Account Maintenance
189 page (%2$s), or disable them altogether
190 by visiting following link:
193 forge_get_config ('forge_name'),
194 util_make_url('/account/'),
195 util_make_url('/account/unsubscribe.php?ch=_'.$row['confirm_hash'])) ;
199 util_send_message($row['email'],$subj, $body."\r\n".$tail,'noreply@'.$sys_default_domain);
200 $last_userid = $row['user_id'];
205 db_query_params ('UPDATE massmail_queue SET failed_date=0, last_userid=$1, finished_date=$2 WHERE id=$3',
211 $err .= $sql.db_error();
213 $mess = "massmail $compt mails sent";
216 function m_exit($mess = '') {
219 if (!cron_remove_lock(__FILE__)) {
220 $err .= "Could not remove lock\n";
222 if (!cron_entry(6,$mess.$err)) {
223 # rely on crond to report the error
224 echo "cron_entry error: ".db_error()."\n";
231 // c-file-style: "bsd"