Fix #453: Feed promotion groups with everybody and ignore pending users in group...
[platal.git] / include / xnet / mail.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22require_once 'diogenes/diogenes.hermes.inc.php';
23
24// {{{ get_all_redirects
25
4370c7eb 26function get_all_redirects($membres, $mls, &$client)
0337d704 27{
28 global $globals;
29
30 $tos = array();
31
32 if ($membres) {
08cce2ff 33 $res = XDB::query(
0337d704 34 'SELECT IF(u.nom <> "", u.nom, m.nom) AS nom,
35 IF(u.prenom <> "", u.prenom, m.prenom) AS prenom,
36 IF(m.email <> "", m.email, CONCAT(a.alias, "@polytechnique.org")) as email,
0cc4c07d 37 IF(m.sexe IS NULL, FIND_IN_SET("femme", u.flags), m.sexe) AS sexe
0337d704 38 FROM groupex.membres AS m
39 LEFT JOIN auth_user_md5 AS u ON (m.uid=u.user_id AND m.uid<50000)
40 LEFT JOIN aliases AS a ON (a.id=u.user_id and a.type="a_vie")
88198bd8 41 WHERE asso_id = {?}
42 AND (m.email <> "" OR u.perms <> "pending")', $globals->asso('id'));
0337d704 43 $tos = $res->fetchAllAssoc();
44 }
45
46 foreach ($mls as $ml) {
47 if (list(,$members) = $client->get_members($ml)) {
48 foreach ($members as $mem) {
575dd9be 49 list($m, $dom) = explode('@',$mem[1]);
0337d704 50 if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
08cce2ff 51 $res = XDB::query('SELECT prenom, nom, FIND_IN_SET("femme", u.flags) AS sexe
8cc026af 52 FROM auth_user_md5 AS u
53 INNER JOIN aliases AS a ON u.user_id = a.id
54 WHERE a.alias = {?}', $m);
0337d704 55 if ($person = $res->fetchOneAssoc()) {
56 $person['email'] = $mem[1];
57 $tos[] = $person;
58 }
59 } else {
0cc4c07d 60 $res = XDB::query('SELECT prenom, nom, sexe FROM groupex.membres WHERE email={?}', $mem[1]);
0337d704 61 if ($person = $res->fetchOneAssoc()) {
62 $person['email'] = $mem[1];
63 $tos[] = $person;
64 } else {
65 $tos[] = array('email' => $mem[1]);
66 }
67 }
68 }
69 }
70 }
71
72 return $tos;
73}
74
75// }}}
76// {{{ _send_xnet_mail
77
9898b36e 78function _send_xnet_mail($user, $body, $mailer, $replyto = null)
0337d704 79{
80 $cher = isset($user['sexe']) ? ($user['sexe'] ? 'Chère' : 'Cher') : 'Cher(e)';
81 $nom = isset($user['nom']) ? $user['nom'] : "";
82 $pnom = isset($user['prenom']) ? $user['prenom'] : preg_replace('!@.*!', '', $user['email']);
83 $to = isset($user['prenom']) ? "\"{$user['prenom']} {$user['nom']}\" <{$user['email']}>" : $user['email'];
84
85 $text = $body;
86 $text = preg_replace('!<cher>!i', $cher, $text);
87 $text = preg_replace('!<nom>!i', $nom, $text);
88 $text = preg_replace('!<prenom>!i', $pnom, $text);
89
90 $mailer->addHeader('To', $to);
9898b36e 91 if ($replyto) {
92 $mailer->addHeader('Reply-To', $replyto);
93 }
0337d704 94 $mailer->setTxtBody(wordwrap($text, 72));
95 $mailer->send();
96}
97
98// }}}
99// {{{ send_xnet_mails
100
9898b36e 101function send_xnet_mails($from, $sujet, $body, $tos, $replyto = null)
0337d704 102{
bdccfd95 103 global $globals;
0337d704 104 $sent = array();
105
106 $mailer = new HermesMailer();
107 $mailer->setSubject($sujet);
108 $mailer->setFrom($from);
bdccfd95 109 $mailer->addHeader('X-Xorg-Login', S::v('bestalias') . '@' . $globals->mail->domain);
0337d704 110
111 foreach ($tos as $user) {
112 if ($sent[$user['email']]) continue;
9898b36e 113 _send_xnet_mail($user, $body, $mailer, $replyto);
0337d704 114 $sent[$user['email']] = true;
115 }
116}
117
118// }}}
119
120// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
121?>