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. FusionForge is free software;
16 * you can redistribute it and/or modify it under the terms of the
17 * GNU General Public License as published by the Free Software
18 * Foundation; either version 2 of the Licence, or (at your option)
21 * FusionForge 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 along
27 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 require dirname(__FILE__).'/../www/env.inc.php';
32 require_once $gfcommon.'include/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 $all_users = user_get_active_users () ;
50 sortUserList ($all_users, 'id') ;
52 $mail_res = db_query_params ('SELECT *
55 ORDER BY queued_date',
59 /* If there was error, notify admins, but don't be pesky */
61 $err .= "cannot execute query to select pending mailings: ".db_error()."\n";
62 $hrs = time()/(60*60);
63 // Send reminder every second day at 11am
64 if (($hrs%24)==11 && (($hrs/24)%2)==1) {
67 forge_get_config('admin_email'),
68 "ATT: Problems with massmail cron script",
69 "This is automatically generated message from\n
70 the mass mailing cron script of ".forge_get_config ('forge_name')."\n
71 installation. There was error querying massmail_queue\n
72 database table. Please take appropriate actions.\n"
78 // $err .= "Got ".db_numrows($mail_res)." rows\n";
80 if (db_numrows($mail_res)<1) {
85 $type = db_result($mail_res, 0, 'type');
87 $err .= "Unknown mailing type\n";
91 $subj = db_result($mail_res, 0, 'subject');
92 $mail_id = db_result($mail_res, 0, 'id');
93 $body = db_result($mail_res, 0, 'message');
94 //$err .= "Got mail to send: ".$subj."\n";
96 $filtered_users = array () ;
98 foreach ($all_users as $user) {
105 $process = $user->getMailingPrefs('site') ;
108 $process = $user->getMailingPrefs('va') ;
111 $process = count ($user->getGroups()) ;
114 foreach ($user->getGroups(false) as $g) {
115 if (forge_check_perm_for_user ($user,'project_admin',$g->getID())) {
122 $process = forge_check_global_perm_for_user ($user,'forge_admin') ;
126 $filtered_users[] = $user ;
130 $err .= "Mailing ".count($filtered_users)." users.\n";
132 // If no more users left, we've finished with this mailing
133 if (count ($filtered_users)==0) {
134 db_query_params ('UPDATE massmail_queue SET failed_date=0,finished_date=$1 WHERE id=$2',
140 // Actual mailing loop
142 foreach ($filtered_users as $user) {
144 if ($type=='SITE' || $type=='COMMNTY') {
145 $tail = "\r\n==================================================================\r\n" ;
146 $tail .= sprintf (_('You receive this message because you subscribed to %1$s
147 site mailing(s). You may opt out from some of them selectively
148 by logging in to %1$s and visiting your Account Maintenance
149 page (%2$s), or disable them altogether
150 by visiting following link:
153 forge_get_config ('forge_name'),
154 util_make_url('/account/'),
155 util_make_url('/account/unsubscribe.php?ch=_'.$user->getConfirmHash())) ;
159 util_send_message($user->getEmail(),$subj, $body."\r\n".$tail,'noreply@'.forge_get_config('web_host'));
160 $last_userid = $user->getID();
165 db_query_params ('UPDATE massmail_queue SET failed_date=0, last_userid=$1, finished_date=$2 WHERE id=$3',
171 $err .= $sql.db_error();
173 $mess = "massmail $compt mails sent";
176 function m_exit($mess = '') {
179 if (!cron_remove_lock(__FILE__)) {
180 $err .= "Could not remove lock\n";
182 if (!cron_entry(6,$mess.$err)) {
183 // rely on crond to report the error
184 echo "cron_entry error: ".db_error()."\n";
191 // c-file-style: "bsd"