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
13 * Copyright 2010, Roland Mas
15 * This file is part of FusionForge.
17 * FusionForge is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * FusionForge is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with FusionForge; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
32 require dirname(__FILE__).'/../www/env.inc.php';
33 require_once $gfcommon.'include/pre.php';
34 require $gfcommon.'include/cron_utils.php';
38 if (!cron_create_lock(__FILE__)) {
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 // Pause between messages, sec
50 $all_users = user_get_active_users () ;
51 sortUserList ($all_users, 'id') ;
53 $mail_res = db_query_params ('SELECT *
56 ORDER BY queued_date',
60 /* If there was error, notify admins, but don't be pesky */
62 $err .= "cannot execute query to select pending mailings: ".db_error()."\n";
63 $hrs = time()/(60*60);
64 // Send reminder every second day at 11am
65 if (($hrs%24)==11 && (($hrs/24)%2)==1) {
68 forge_get_config('admin_email'),
69 "ATT: Problems with massmail cron script",
70 "This is automatically generated message from\n
71 the mass mailing cron script of ".forge_get_config ('forge_name')."\n
72 installation. There was error querying massmail_queue\n
73 database table. Please take appropriate actions.\n"
79 // $err .= "Got ".db_numrows($mail_res)." rows\n";
81 if (db_numrows($mail_res)<1) {
86 $type = db_result($mail_res, 0, 'type');
88 $err .= "Unknown mailing type\n";
92 $subj = db_result($mail_res, 0, 'subject');
93 $mail_id = db_result($mail_res, 0, 'id');
94 $body = db_result($mail_res, 0, 'message');
95 //$err .= "Got mail to send: ".$subj."\n";
97 $filtered_users = array () ;
99 foreach ($all_users as $user) {
106 $process = $user->getMailingPrefs('site') ;
109 $process = $user->getMailingPrefs('va') ;
112 $process = count ($user->getGroups()) ;
115 foreach ($user->getGroups(false) as $g) {
116 if (forge_check_perm_for_user ($user,'project_admin',$g->getID())) {
123 $process = forge_check_global_perm_for_user ($user,'forge_admin') ;
127 $filtered_users[] = $user ;
131 $err .= "Mailing ".count($filtered_users)." users.\n";
133 // If no more users left, we've finished with this mailing
134 if (count ($filtered_users)==0) {
135 db_query_params ('UPDATE massmail_queue SET failed_date=0,finished_date=$1 WHERE id=$2',
141 // Actual mailing loop
143 foreach ($filtered_users as $user) {
145 if ($type=='SITE' || $type=='COMMNTY') {
146 $tail = "\r\n==================================================================\r\n" ;
147 $tail .= sprintf (_('You receive this message because you subscribed to %1$s
148 site mailing(s). You may opt out from some of them selectively
149 by logging in to %1$s and visiting your Account Maintenance
150 page (%2$s), or disable them altogether
151 by visiting following link:
154 forge_get_config ('forge_name'),
155 util_make_url('/account/'),
156 util_make_url('/account/unsubscribe.php?ch=_'.$user->getConfirmHash())) ;
160 util_send_message($user->getEmail(),$subj, $body."\r\n".$tail,'noreply@'.forge_get_config('web_host'));
161 $last_userid = $user->getID();
166 db_query_params ('UPDATE massmail_queue SET failed_date=0, last_userid=$1, finished_date=$2 WHERE id=$3',
172 $err .= $sql.db_error();
174 $mess = "massmail $compt mails sent";
177 function m_exit($mess = '') {
180 if (!cron_remove_lock(__FILE__)) {
181 $err .= "Could not remove lock\n";
183 if (!cron_entry(6,$mess.$err)) {
184 // rely on crond to report the error
185 echo "cron_entry error: ".db_error()."\n";
192 // c-file-style: "bsd"