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