9380a79c988b15bf923e8447c4cc17b6df13c177
[platal.git] / include / xnet / mail.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2004 Polytechnique.org *
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
22 require_once 'diogenes/diogenes.hermes.inc.php';
23
24 // {{{ get_all_redirects
25
26 function get_all_redirects($membres, $mls, &$client)
27 {
28 global $globals;
29
30 $tos = array();
31
32 if ($membres) {
33 $res = $globals->xdb->query(
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,
37 FIND_IN_SET("femme", u.flags) AS sexe
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")
41 WHERE asso_id = {?}', $globals->asso('id'));
42 $tos = $res->fetchAllAssoc();
43 }
44
45 foreach ($mls as $ml) {
46 if (list(,$members) = $client->get_members($ml)) {
47 foreach ($members as $mem) {
48 list($m, $dom) = split('@',$mem[1]);
49 if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
50 $res = $globals->xdb->query('SELECT prenom, nom, FIND_IN_SET("femme", u.flags) AS sexe
51 FROM auth_user_md5 AS u
52 INNER JOIN aliases AS a ON u.user_id = a.id
53 WHERE a.alias = {?}', $m);
54 if ($person = $res->fetchOneAssoc()) {
55 $person['email'] = $mem[1];
56 $tos[] = $person;
57 }
58 } else {
59 $res = $globals->xdb->query('SELECT prenom, nom FROM groupex.membres WHERE email={?}', $mem);
60 if ($person = $res->fetchOneAssoc()) {
61 $person['email'] = $mem[1];
62 $tos[] = $person;
63 } else {
64 $tos[] = array('email' => $mem[1]);
65 }
66 }
67 }
68 }
69 }
70
71 return $tos;
72 }
73
74 // }}}
75 // {{{ _send_xnet_mail
76
77 function _send_xnet_mail($user, $body, $mailer)
78 {
79 $cher = isset($user['sexe']) ? ($user['sexe'] ? 'Chère' : 'Cher') : 'Cher(e)';
80 $nom = isset($user['nom']) ? $user['nom'] : "";
81 $pnom = isset($user['prenom']) ? $user['prenom'] : preg_replace('!@.*!', '', $user['email']);
82 $to = isset($user['prenom']) ? "\"{$user['prenom']} {$user['nom']}\" <{$user['email']}>" : $user['email'];
83
84 $text = $body;
85 $text = preg_replace('!<cher>!i', $cher, $text);
86 $text = preg_replace('!<nom>!i', $nom, $text);
87 $text = preg_replace('!<prenom>!i', $pnom, $text);
88
89 $mailer->addHeader('To', $to);
90 $mailer->setTxtBody(wordwrap($text, 72));
91 $mailer->send();
92 }
93
94 // }}}
95 // {{{ send_xnet_mails
96
97 function send_xnet_mails($from, $sujet, $body, $tos)
98 {
99 $sent = array();
100
101 $mailer = new HermesMailer();
102 $mailer->setSubject($sujet);
103 $mailer->setFrom($from);
104
105 foreach ($tos as $user) {
106 if ($sent[$user['email']]) continue;
107 _send_xnet_mail($user, $body, $mailer);
108 $sent[$user['email']] = true;
109 }
110 }
111
112 // }}}
113
114 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
115 ?>