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 ('squal_pre.php');
34 require ('common/include/cron_utils.php');
38 if (!cron_create_lock('gforge-massmail')) {
39 $err = "Massmail already running...exiting";
40 if (!cron_entry(6,$err)) {
41 # rely on crond to report the error
42 echo "cron_entry error: ".db_error()."\n";
47 //bad hack to get around Roland's misuse of Language in utils.php
48 $Language = new BaseLanguage();
49 $Language->loadLanguage($sys_lang);
51 // Pause between messages, sec
54 // This tables maps mailing types to tables which required to perform it
55 $table_mapping = array(
59 'DVLPR' => "users,user_group",
60 'ADMIN' => "users,user_group,groups",
61 'SFDVLPR' => "users,user_group",
64 // This tables maps mailing types to WHERE subclauses which select
66 $cond_mapping = array(
68 'SITE' => "AND mail_siteupdates=1",
69 'COMMNTY' => "AND mail_va=1",
70 'DVLPR' => "AND users.user_id=user_group.user_id",
71 '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",
72 'SFDVLPR' => "AND users.user_id=user_group.user_id AND user_group.group_id=1"
75 $mail_res = db_query("SELECT *
78 ORDER BY queued_date");
80 /* If there was error, notify admins, but don't be pesky */
82 $err .= "cannot execute query to select pending mailings: ".db_error()."\n";
83 $hrs = time()/(60*60);
84 // Send reminder every second day at 11am
85 if (($hrs%24)==11 && (($hrs/24)%2)==1) {
86 global $sys_admin_email;
89 "ATT: Problems with massmail cron script",
90 "This is automatically generated message from\n"
91 ."the mass mailing cron script of $sys_name\n"
92 ."installation. There was error querying massmail_queue\n"
93 ."database table. Please take appropriate actions.\n"
99 // $err .= "Got ".db_numrows($mail_res)." rows\n";
101 if (db_numrows($mail_res)<1) {
106 $type = db_result($mail_res, 0, 'type');
107 if (!$table_mapping[$type]) {
108 $err .= "Unknown mailing type\n";
112 $subj = db_result($mail_res, 0, 'subject');
113 $mail_id = db_result($mail_res, 0, 'id');
115 //$err .= "Got mail to send: ".$subj."\n";
117 $sql = "SELECT DISTINCT users.user_id,users.user_name,users.realname,users.email,users.confirm_hash
118 FROM $table_mapping[$type]
119 WHERE users.user_id>".db_result($mail_res, 0, 'last_userid')."
121 ".$cond_mapping[$type]."
122 ORDER BY users.user_id";
126 // Get next chunk of users to mail
127 $users_res = db_query($sql);
129 $err .= "Mailing ".db_numrows($users_res)." users.\n";
131 // If no more users left, we've finished with this mailing
132 if ($users_res && db_numrows($users_res)==0) {
133 db_query("UPDATE massmail_queue
134 SET failed_date=0,finished_date='".time()."'
135 WHERE id='$mail_id'");
139 // These mailing types should include unsubscription info
140 if ($type=='SITE' || $type=='COMMNTY') {
141 $tail = "\r\n==================================================================\r\n"
142 ."You receive this message because you subscribed to $sys_name\r\n"
143 ."site mailing(s). You may opt out from some of them selectively\r\n"
144 ."by logging in to $sys_name and visiting your Account Maintenance\r\n"
145 ."page (http://$sys_default_domain/account/), or disable them altogether\r\n"
146 ."by visiting following link:\r\n"
147 ."<http://$sys_default_domain/account/unsubscribe.php?ch=_%s>\r\n";
149 $body = db_result($mail_res, 0, 'message');
151 // Actual mailing loop
153 while ($row =& db_fetch_array($users_res)) {
155 util_send_message($row['email'],$subj,$body."\r\n".sprintf( $tail,$row['confirm_hash'] ),'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('gforge-massmail')) {
179 $err .= "Could not remove lock file\n";
181 if (!cron_entry(6,$mess.$err)) {
182 # rely on crond to report the error
183 echo "cron_entry error: ".db_error()."\n";