New PlMailer based on Hermes code:
[platal.git] / bin / newsletter.send.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2006 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
22
23 require_once('./connect.db.inc.php');
24 require_once('../classes/plmailer.php');
25 require_once("newsletter.inc.php");
26
27 $opt = getopt('i:h');
28
29 if(empty($opt['i']) || isset($opt['h'])) {
30 echo <<<EOF
31 usage: send_nl.php -i nl_id
32 sends the NewsLetter of id "id"
33 EOF;
34 exit;
35 }
36
37 $id = intval($opt['i']);
38 $nl = new NewsLetter($id);
39 $nl->setSent();
40
41 while(true) {
42 $res = XDB::iterRow(
43 "SELECT ni.user_id, a.alias,
44 u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
45 FIND_IN_SET('femme', u.flags),
46 q.core_mail_fmt AS pref
47 FROM newsletter_ins AS ni
48 INNER JOIN auth_user_md5 AS u USING(user_id)
49 INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
50 INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
51 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
52 WHERE ni.last<{?} AND e.email IS NOT NULL
53 GROUP BY ni.user_id
54 LIMIT 60", $id);
55 if (!$res->total()) { exit; }
56
57 $sent = Array();
58 while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt) = $res->next()) {
59 $sent[] = "user_id='$uid'";
60 $nl->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html');
61 }
62 XDB::execute('UPDATE newsletter_ins SET last={?} WHERE '.implode(' OR ', $sent), $id);
63 sleep(60);
64 }
65
66 ?>